![](/img/trans.png)
[英]Get selected values in a multi-select drop-down and insert them in a JSON
[英]How to get final select values from multi-select drop down list Jquery
這是我的代碼,它將從多選下拉列表中獲取所有選擇的值,包括先前的選擇值。 我的問題是,如何獲得最終選擇價值? 謝謝!
var devices = new Array();
var device = document.getElementById("deviceCat");
$('#deviceCat option:selected').each(function() {
devices.push($(this).text());
});
例如,如果用戶首先選擇1,然后發現他不需要選擇1。那么,他依次選擇2和4,但是我的代碼將輸出1,2和4。 2和4。
干得好:
演示: 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.