を使用して、複数のドロップダウン ボックスで同じ関数を実行していid: assignee, status , priority
ます。
$("#assignee , #status , #priority").change(function() {
//text here
}
しかし、どの特定のドロップダウンがクリックされたかを知りたいです。これを行う方法はありますか?
を使用して、複数のドロップダウン ボックスで同じ関数を実行していid: assignee, status , priority
ます。
$("#assignee , #status , #priority").change(function() {
//text here
}
しかし、どの特定のドロップダウンがクリックされたかを知りたいです。これを行う方法はありますか?
以下を使用するだけです。
$("#assignee, #status, #priority").change(function() {
if (this.id === "assignee") { ... }
});
これを使って。どのイベント ハンドラーでも、これは実際のイベントが発生した要素を指します。
$("#assignee , #status , #priority").change(function() {
var changedElementID = $(this).attr("id");
}
別のオプションは、次を使用することevent.target
です。
$("#assignee , #status , #priority").change(function(e) {
var changedElementID = $(e.target).attr("id");
}
ここでは、その他のイベント オブジェクト プロパティに関する詳細情報を確認できます。
this
とは両方ともe.target
DOM オブジェクトであり、jQuery オブジェクトではないことに注意してください。したがって、jQuery 関数を使用するには、上記のコードのように jquery でラップする必要があります。単純にIDを取得する場合は、直接使用するthis.id
か、e.target.id
どちらが高速かを使用できます。
イベントがトリガーされると、イベント ハンドラー関数内で、イベントthis
をトリガーした要素が参照されるため、次のようになります。
$('#assignee, #status, #priority').change(function(e) {
// Use 'this' to refer to the element that triggered the event
console.log(this.id);
// will output assignee, status or priority depending on which changed
});
<option>
その時点でバインドしている場所をthis
関数に渡し、独自のコードを使用します
$("#assignee , #status , #priority").change(function(boxID) {
console.log($(boxID).val()); //Gives you the value of the selected drop down
//text here
}
ターゲットを使用:
$("#assignee , #status , #priority").change(function(e) {
var changedElementID = $(e.target).attr("id");
}