我怎样才能获得未选择选项的值Select2 https://select2.github.io/ using select2:unselect
$('#mySelect').on("select2:unselect", function(e){
var unselected_value = $('#mySelect').val(); // using this shows 'null'
// or using below expression also shows 'null'
var unselected_value = $('#mySelect :selected').val();
alert(unselected_value);
}).trigger('change');
在上面的代码中警报显示“null”
我需要使用select2:unselect
因为“改变”事件会感知到:select
and :unselect
both.
这是一个较旧的问题,但也许这个答案会对某人有所帮助。我正在使用带有多个值/标签的 Select2 v4.0.3,并且只需要删除特定值/标签的 ID。我真的不想使用unselecting
其他答案中提到的事件。在里面unselect
事件没有args
对象,这样你就可以像这样获取你要删除的对象的 ID...
jQuery('.mySelect2').on("select2:unselecting", function(e){
return true; // I don't use this unselecting event but here is how you could use it to get the ID of the one you are trying to remove.
console.log(e);
console.log(e.params);
console.log(e.params.args.data);
console.log(e.params.args.data.id);
//console.log(e.params.args.data.tag); data.tag is specific to my code.
});
jQuery('.mySelect2').on('select2:unselect', function (e) {
console.log(e);
console.log(e.params);
console.log(e.params.data);
console.log(e.params.data.id);
//console.log(e.params.data.tag); data.tag is specific to my code.
/*
Now you can do an ajax call or whatever you want for the specific
value/tag that you are trying to remove which is: e.params.data.id.
*/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)