0

を使用して、複数のドロップダウン ボックスで同じ関数を実行していid: assignee, status , priorityます。

$("#assignee , #status , #priority").change(function() { 

//text here

}

しかし、どの特定のドロップダウンがクリックされたかを知りたいです。これを行う方法はありますか?

4

5 に答える 5

2

以下を使用するだけです。

$("#assignee, #status, #priority").change(function() { 
    if (this.id === "assignee") { ... }
});
于 2013-02-06T09:40:34.913 に答える
2

これを使って。どのイベント ハンドラーでも、これは実際のイベントが発生した要素を指します。

$("#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.targetDOM オブジェクトであり、jQuery オブジェクトではないことに注意してください。したがって、jQuery 関数を使用するには、上記のコードのように jquery でラップする必要があります。単純にIDを取得する場合は、直接使用するthis.idか、e.target.idどちらが高速かを使用できます。

于 2013-02-06T09:40:52.357 に答える
1

イベントがトリガーされると、イベント ハンドラー関数内で、イベント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
});
于 2013-02-06T09:40:52.323 に答える
0

<option>その時点でバインドしている場所をthis関数に渡し、独自のコードを使用します

$("#assignee , #status , #priority").change(function(boxID) { 
console.log($(boxID).val());    //Gives you the value of the selected drop down


//text here

}
于 2013-02-06T09:44:20.177 に答える
0

ターゲットを使用:

$("#assignee , #status , #priority").change(function(e) { 
        var changedElementID = $(e.target).attr("id");
    }
于 2013-02-06T09:41:35.520 に答える