[英]jQuery same function for multiple events, each event targeting different elements?
測試功能中的位置。
$(document).on("change click", "select option", function(e) {
if ($(this).index() == 0) {
// do stuff for first child
} else {
// do stuff for other children
});
但是,我不認為change
事件是在<option>
元素上觸發的,它只是在<select>
本身上觸發的。 所以您的基本前提似乎是錯誤的,因為change
事件永遠不會在select:nth-child(1)
上觸發。
您有一個答案,但也可以使用以下方法的組合:
注意:轉到頁面上的元素以觸發focusin
事件
$('#selectcontainer').on('change focusin manual', ".myselectthing", function(e) {
var selectionIndex = $(this).prop('selectedIndex');
$('#results').text("this type " + e.type + ":" + $(this).val() + " : " + selectionIndex);
});
$('#selectcontainer').find('.myselectthing').val("ralf").trigger("manual");
一些示例標記:
<div id="results">
empty
</div>
<div id="selectcontainer">
<select class="myselectthing">
<option value="me">choose me</option>
<option value="ralf">choose ralf</option>
<option value="charlie">choose charlie</option>
</select>
</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.