[英]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.