简体   繁体   中英

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 .

Here you go:

Demo: JSFiddle

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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