简体   繁体   English

如何从多选下拉列表中获取最终选择值

[英]How to get final select values from multi-select drop down list Jquery

Here is my code, it will get all selected values from multi-select drop down list including the previous select values. 这是我的代码,它将从多选下拉列表中获取所有选择的值,包括先前的选择值。 My question is, how to get final select valus? 我的问题是,如何获得最终选择价值? Thank you! 谢谢!

var devices = new Array();
var device = document.getElementById("deviceCat");
        $('#deviceCat option:selected').each(function() {
            devices.push($(this).text());
        });

For example, if user select 1 first, and then he find that he doesn't need to select 1. So, he turn to select 2 and 4 then, but my code will output 1,2 and 4. What I want is only 2 and 4 . 例如,如果用户首先选择1,然后发现他不需要选择1。那么,他依次选择2和4,但是我的代码将输出1,2和4。 2和4。

Here you go: 干得好:

Demo: JSFiddle 演示: JSFiddle

$("#mySelect").on("change",function(){
    var devices = [];
    $('#mySelect :selected').each(function(i, selected){ 
      devices[i] = $(selected).text();
      //OR
      //devices.push($(selected).text()); 
    });
    alert(devices);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM