如何在Select2中使用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
,因为'change‘事件将同时感知:select
和:unselect
。
发布于 2015-12-25 15:32:42
最后,我得到了解决方案,即:未选择选项的值只有在未选择之前才可用。现在不是在select2:unselect
,而是在select2:unselecting
,代码将是:
$('#mySelect').on("select2:unselecting", function(e){
var unselected_value = $('#mySelect').val();
alert(unselected_value);
}).trigger('change');
发布于 2017-01-13 19:12:13
这是一个较老的问题,但也许这个答案会对某人有所帮助。我使用的是带有多个值/标记的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.
*/
发布于 2016-03-24 15:52:08
实际上,数据值在事件对象中。如果您处理的是select2多个值,这将是有用的。
function(e){
console.log(e.params.data)
}
https://stackoverflow.com/questions/34463714
复制相似问题