Jquery Multiselect : How to know which value was selected/deselected

丶灬走出姿态 提交于 2020-01-03 15:59:04

问题


I have Multiselect dropdown. Whenever there is a select or deselect, I need to get the value. I am using change event, but struggling to get which option was selected/deselected.


回答1:


//all options

var all=[];
$('#multiple').each(function(i, selected){ 
    all[i] = $(selected).text(); 
});

//selected options  
var foo = []; 
$('#multiple :selected').each(function(i, selected){ 
    foo[i] = $(selected).text(); 
});

// unselected options
var de= $.grep(all, function(element) {
    return $.inArray(element, foo) !== -1;
});

In the foo array are the selected values

In the de array are the unselected values




回答2:


Try this,

$('#selectId').on('change',function(){ 
    console.log($(this).val());// this will give you an array
});


来源:https://stackoverflow.com/questions/18627643/jquery-multiselect-how-to-know-which-value-was-selected-deselected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!