如何获取多选中最后选择的选项?
$('#select').change(function(event) {
...
});
我不需要 val()
提供的所有值,但需要最新选择的触发更改
的选项。
谢谢
最佳答案
我担心即使使用 jQuery,您也不能如此轻松地拥有它。但您可以存储旧值并与当前值进行比较。
var last = [];
$('#select').change(function(event) {
var val = $(this).val();
var newValues = val.filter(function(element)) {
// You may need a more specific test for your values
return last.indexOf(element) == -1;
});
// newValues are the new selected options in the select
last = val;
});
但是警告:如果用户取消选项,change
事件也会被触发。 newValues
将为空(因为没有新值,只有缺失值)。
关于javascript - 使用 jQuery 获取多选的最新选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32714998/