Select2事件多次触发



在各种情况下,我都会动态附加下拉列表。现在,在一个多选下拉列表中,我尝试使用select2:select事件获取最新选择的值。当触发此事件时,我会为单个操作获取多个日志。

这是完整的代码:

$(document).on('change', '.optionsDropDown', function (e) {
$('#myid').on('select2:select', function (e) {
var data = e.params.data;
console.log(data)
})
$('#myid').on('select2:unselect', function (e) {
var data = e.params.data;
console.log(data)
})
})

它适用于未动态附加的下拉列表。

正如注释中所建议的,是事件循环造成了问题。

这就是对我有效的方法。

$(document).on('select2:select', '.dropDownClass', function (e) {
var data = e.params.data;
//logic...
})

此解决方案适用于我:

$('#element').off('select2:unselect').on('select2:unselect', function (e) {
// Your event code here
});

您只需要在将事件绑定到";在"上";。

这里还有更多的例子:

$('#element').off('select2:select').on('select2:unselect', function (e) {
// Your event code here
});

最新更新