在jQuery选择列表选项通过另一个元素onchange事件更改后引发事件



我有一个选择列表(#select2),每次更改另一个选择名单(#selected1)时,其选项都会更改。

select2的选项始终取决于#select1的值,并且每次更改#select1。

有没有一个函数可以绑定到#select2元素,它将在选项列表更改完成后激发(每次它都受到#select1的影响)?

我似乎无法向select1元素添加onchange处理程序来执行我想要的操作,因为它似乎在select2的选项列表完成之前启动。

您可以使用trigger()bind()方法来引发和侦听自定义事件:

试试这个:

$("#select1").change(function() {
    // update options in #select2
    $("#select2").trigger("updatecomplete");
});
$("#select2").bind("updatecomplete", function() {
    // this will fire when #select1 change event has finished updating the options.
});

有关触发器()的更多信息


更新的答案-2016年9月

请注意,bind()现在已弃用。您应该使用on()

$("#select2").on("updatecomplete", function() {
    // this will fire when #select1 change event has finished updating the options.
});

通过javascript所做的更改不会引发事件。你需要在改变它的代码中自己触发它。

$("#select2").change();

最新更新