की तरह जवाब में के रूप में this.value
का उपयोग करते हुए ऊपर में विफल रहता है जब उपयोगकर्ता के लिए Ctrl + क्लिक किया और एक से अधिक आइटम का चयन किया है - यह सूची में पहले चयन का मान देता है, यहां तक कि कि अगर आखिरी क्लिक नहीं किया गया था। इसे आज़माएं:
var previouslySelected = [];
$("#myMultiselect").change (function() {
// Get newly selected elements
var currentlySelected = $(this).val();
var newSelections = currentlySelected.filter(function (element) {
return previouslySelected.indexOf(element) == -1;
});
previouslySelected = currentlySelected;
if (newSelections.length) {
// If there are multiple new selections, we'll take the last in the list
var lastSelected = newSelections.reverse()[0];
}
});
आपको अंतिम चयनित आइटम का ट्रैक रखना होगा। –