Sử dụng this.value
như trong câu trả lời ở trên không thành công khi người dùng đã Ctrl + nhấp và chọn nhiều mục - nó trả về giá trị của sự lựa chọn đầu tiên trong danh sách, thậm chí nếu điều đó không phải là lần nhấp cuối cùng. Hãy thử điều này:
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];
}
});
Bạn phải tự theo dõi mục được chọn cuối cùng. –