select2:unselect 无法正常工作
select2:unselect doesn't work properly
我的 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)
});
我需要获取当前值,如果未选择任何内容,我会期望为空。
有没有正确的方法获取当前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)
});
我的 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)
});
我需要获取当前值,如果未选择任何内容,我会期望为空。
有没有正确的方法获取当前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)
});