gpt4 book ai didi

javascript - 选择2 :unselect doesn't work properly

转载 作者:行者123 更新时间:2023-11-28 05:54:47 34 4
gpt4 key购买 nike

当我用鼠标单击十字符号时,我遇到了 Select2 问题(请注意:allowClear 为 true)select2:unselect 事件被触发,但之前选择的值仍保持选择状态一段时间。

<select id='select2'>
<option value='1'>option 1</option>
</select>

$("#select2").select2({
placeholder: "placeholder",
dropdownAutoWidth: true,
allowClear: true
})


$("#select2").on("select2:select select2:unselect", function (e) {

var currentValue = $(this).val();

console.log($(this).val());
setTimeout(function () {
console.log($("#select2").val());
}, 2000)

});

我需要获取当前值,如果没有选择任何内容,我会期望为 null。

是否有正确的方法来获取当前的 select2 值?

这是演示 - jsfiddle

最佳答案

试试这个:

$("#select2").on("change", function (e) {

var currentValue = $(this).val(); // I want null when unselect is fired

console.log($(this).val());
setTimeout(function () {
console.log($("#select2").val());
}, 2000)

});

关于javascript - 选择2 :unselect doesn't work properly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37793569/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com