我有一个 select2
select 元素,我通过 jQuery 设置其值
$("#select2").val(elment_id).trigger('change');
但是,我已经定义了一个 onchange 事件处理程序。
$('#select2').on('change', function(e) {
//TODO
});
我只想更改(设置)select2
的值而不触发更改事件。有什么想法,怎么做吗?
最佳答案
有一个特殊事件可以刷新它:
$("#select2").val(elment_id).trigger('change.select2');
来自文档:
It's common for other components to be listening to the change event, or for custom event handlers to be attached that may have side effects. To limit the scope to only notify Select2 of the change, use the .select2 event namespace:
$('#mySelect2').val('US'); // Change the value or make some change to the internal state $('#mySelect2').trigger('change.select2'); // Notify only Select2 of changes
关于javascript - 如何在不触发事件的情况下改变select2的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55897877/