问题
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