Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在multiselect下拉列表jquery中获取最近选择的选项的索引_Javascript_Jquery_Html - Fatal编程技术网

Javascript 在multiselect下拉列表jquery中获取最近选择的选项的索引

Javascript 在multiselect下拉列表jquery中获取最近选择的选项的索引,javascript,jquery,html,Javascript,Jquery,Html,我的html页面中有一个选择下拉列表。如何获取最近选择的选项的索引,而不是最后一个选项的索引。我看到我们可以使用“last”,但它会给出最后一个选项,该选项不是最近选择的 HTML 如果我们选择“欧宝”,然后选择“沃尔沃”,我们应该得到零作为指数。但现在它给出了“欧宝”指数 不要使用.change()事件,而是使用选项上的。单击() $(函数(){ $('.carMenu选项')。单击(函数(){ 如果($(this).is(':selected')){ log($(this.index())

我的html页面中有一个选择下拉列表。如何获取最近选择的选项的索引,而不是最后一个选项的索引。我看到我们可以使用
“last”
,但它会给出最后一个选项,该选项不是最近选择的

HTML


如果我们选择“欧宝”,然后选择“沃尔沃”,我们应该得到零作为指数。但现在它给出了“欧宝”指数

不要使用
.change()
事件,而是使用选项上的
。单击()

$(函数(){
$('.carMenu选项')。单击(函数(){
如果($(this).is(':selected')){
log($(this.index());
}
});
});

沃尔沃汽车
萨博
欧宝
奥迪

每次,我都会得到包含所有选定选项值的
carMenu
值,然后将其与上次比较,以检测最近添加的项目。代码还支持取消选择选项。事实上,它保留了一堆
索引
,并在取消选择时弹出
索引
。请尝试以下代码:

var recent_options = selected_options = added_options = removed_options = [];
var current_values, index;
$(document).ready(function () {
    $('.carMenu').change(function(){
        current_values = $(this).val();
        if (current_values && current_values.length > 0) {
            added_options = current_values.filter(function(x) { return recent_options.indexOf(x) < 0 });
            if (added_options.length > 0) {
                selected_options.push($(this).find("option[value='" + added_options[0] + "']").index());
                console.log("last selected option is : " + selected_options[selected_options.length-1]);
            }
            else {
                removed_options = recent_options.filter(function(x) { return current_values.indexOf(x) < 0 });
                if (removed_options.length > 0) {   
                    index = selected_options.indexOf($(this).find("option[value='" + removed_options[0] + "']").index());
                    if (index > -1)
                        selected_options.splice(index, 1);
                    console.log("last selected option is : " + selected_options[selected_options.length-1]);
                }
            }
        }
        recent_options = current_values ? current_values : [];
    });
});
var-recented\u options=selected\u options=added\u options=removed\u options=[];
var当前_值,指数;
$(文档).ready(函数(){
$('.carMenu').change(函数(){
当前_值=$(this.val();
if(当前值和当前值长度>0){
添加的_选项=当前的_值.filter(函数(x){返回最近的_选项.indexOf(x)<0});
如果(添加了_options.length>0){
所选的_options.push($(this.find)(“选项[value=”+“添加的_options[0]+“]”)index());
log(“上次选择的选项是:”+选择的_选项[selected_options.length-1]);
}
否则{
移除的_选项=最近的_选项.filter(函数(x){返回当前的_值.indexOf(x)<0});
如果(已删除的选项长度>0){
index=selected_options.indexOf($(this).find(“option[value='”+removed_options[0]+'”)).index();
如果(索引>-1)
所选选项。拼接(索引,1);
log(“上次选择的选项是:”+选择的_选项[selected_options.length-1]);
}
}
}
最近的_选项=当前_值?当前_值:[];
});
});

沃尔沃汽车
萨博
欧宝
奥迪
如果您不想支持取消选择选项,只需删除
else
部分
If(added_options.length>0)

也要注意


jsiddle:

网页本身并不保留旧表单输入值的“内存”。您必须使用
.change()
将每个选择存储在一个变量中。您甚至可以使用我在Chrome48上使用jquery-2.2.0.min.js尝试过的代码将此选择历史附加到元素本身,我看到了正确的索引。我不确定我是否理解这个问题?对您试图解决的更高级别问题的更好解释将有助于请注意,用户可以在多次选择中一次选择多个
,因此
更改
可能反映出与以前的
更改
有许多不同,您最近的意思是什么?您的意思是单击选项?很惊讶您不知道跨浏览器不支持选项上的
单击
。当然,这在IE中是没有用的,而且我认为Safari也是如此。似乎OP也想知道以前的选择有什么变化
$(document).ready(function () {
    $('.carMenu').change(function(){
         console.log($('option:selected',$(this)).index());
    });
});
var recent_options = selected_options = added_options = removed_options = [];
var current_values, index;
$(document).ready(function () {
    $('.carMenu').change(function(){
        current_values = $(this).val();
        if (current_values && current_values.length > 0) {
            added_options = current_values.filter(function(x) { return recent_options.indexOf(x) < 0 });
            if (added_options.length > 0) {
                selected_options.push($(this).find("option[value='" + added_options[0] + "']").index());
                console.log("last selected option is : " + selected_options[selected_options.length-1]);
            }
            else {
                removed_options = recent_options.filter(function(x) { return current_values.indexOf(x) < 0 });
                if (removed_options.length > 0) {   
                    index = selected_options.indexOf($(this).find("option[value='" + removed_options[0] + "']").index());
                    if (index > -1)
                        selected_options.splice(index, 1);
                    console.log("last selected option is : " + selected_options[selected_options.length-1]);
                }
            }
        }
        recent_options = current_values ? current_values : [];
    });
});