Javascript 在multiselect中获取当前选项

Javascript 在multiselect中获取当前选项,javascript,jquery,Javascript,Jquery,如何在Jquery multiselect中获取当前选中/未选中的 我已经知道如何在multiselect中获取选定值。问题是,我需要获取用户单击时当前选中/未选中的一个。这看起来是个微不足道的问题,但到目前为止我还没有找到一个合适的解决方案 这是我以前尝试过的: $('#mySelect').change(function(){ var element = $(this).val(); }) 不幸的是,它返回一个包含所有选定选项的数组。我需要的是获得用户选择或未选择的当前值。我只是试着

如何在Jquery multiselect中获取当前选中/未选中的

我已经知道如何在multiselect中获取选定值。问题是,我需要获取用户单击时当前选中/未选中的一个。这看起来是个微不足道的问题,但到目前为止我还没有找到一个合适的解决方案

这是我以前尝试过的:

$('#mySelect').change(function(){
   var element = $(this).val();
})
不幸的是,它返回一个包含所有选定选项的数组。我需要的是获得用户选择或未选择的当前值。我只是试着自己用辅助变量创建一个方法,现在似乎可以了

    if($(this).attr('id').includes('Type')) Key = "trackertype";

    if(selectedValue.length > 0 && $(this).val() == null){
        Value = selectedValue[0].toString();
        selectedValue = [];
        Key = '-' + Key;
    }else if(selectedValue.length == 0 && $(this).val() != null){
        selectedValue.push($(this).val()[0]);
        Value = selectedValue[0].toString();
    }else if(selectedValue.length > 0 && $(this).val() != null && selectedValue.length < $(this).val().length){
        var selected = true;
        $.each($(this).val(),function(i,item){
            var current = item.toString();
            $.each(selectedValue,function(i,itemV){
                if(current != itemV.toString()){
                    Value = current.toString();  
                    selectedValue.push(current.toString());  
                    selected = false;                                   
                }
            });
        });
    }else if(selectedValue.length > $(this).val().length){
        $.each($(this).val(),function(i,item){
            var current = item.toString();
            $.each(selectedValue,function(i,itemV){
                if(current != itemV.toString()){
                    Value = itemV.toString();  
                    selectedValue = jQuery.grep(selectedValue, function(value) {
                        return value != itemV.toString();
                    });
                    selected = true;                                   
                }
            });
        });
    }
    if(selected){
        Key = '-' + Key;
    }
我需要将当前所选选项的Valuevalue和Keyselect名称发送到web服务。这不是最好的代码抱歉,但它确实做了它的工作

谢谢

当任何html元素都可以使用document.getElementsById'id'获取时,这很简单

正如您希望使用css类或任何css选择器而不是元素id或html元素名称获取html元素一样

例如:

<p class="text-center" > abcd </p> 
同样,在给定的示例中,我们需要为我使用的属性选择的选项html元素:

document.querySelectorAll('option:checked'); 

到目前为止您做了什么?可能重复了我使用$'selectId.val尝试过的Yes,但这会返回一个选定值的数组。我只需要用户选择/取消选择的最后一个选项。@CarlosIS您可能希望删除您的问题,并包含一些代码和注释中的信息。您是否尝试过在选项上添加click listener?当任何html元素都可以使用document.getElementsById'id获取时,这很简单。我想在您的回答中解释一下。。。你可以随时编辑它!当任何html元素都可以使用document.getElementsById'id'获取时,这很简单。正如您希望使用css类或任何css选择器而不是元素id或html元素名称获取html元素一样。例如:

abcd

我们可以使用queryselectorAll.text center检索所有html。在给定的示例中,我们需要option html元素,它是为我使用的document属性选择的;把这个写在你的答案里。。。单击“编辑”,您可以对其进行编辑。不要把细节写在评论里,不是每个人都会读的…你的编辑。我又编辑了一遍,使它变得更好。谢谢你的时间,这样你可以帮助更多的人,你的答案可能会得到更多的选票。
document.querySelectorAll('.text-center')
document.querySelectorAll('option:checked');