簡體   English   中英

從“選擇”框中獲取所有選項(選中和未選中)

[英]Get all options from Select box (selected and non selected)

使用此代碼:

$('#select-from').each(function() {
  alert($(this).val());
});

<select name="selectfrom" id="select-from" multiple="" size="15">
  <option value="1">Porta iPhone Auto</option>
  <option value="2">Leather Moto Vest</option
</select>

我從選擇框中獲得了所有選定的值,我如何還獲得未選定的值?

試試下面

獲得所有價值

var valuesArray = $("#select-from option").map(function(){
  return this.value;
}).get();

獲取不同數組中的選定值和未選定值。

var values = {
  selected: [],
  unselected:[]
};

$("#select-from option").each(function(){
  values[this.selected ? 'selected' : 'unselected'].push(this.value);
});

謝謝,

西瓦

這是您需要的代碼:

 $('#select-from option').each(function(){
    alert($(this).val());
});

看看這個jsfiddle

$("#selectId > option").each(function() {
    alert(this.text + ' ' + this.value);
});

this.text將給出文本

this.value將給出值

我的版本:)

$("#select-from option:selected").each(function (i, x) {
    alert($(x).val() + " selected");
});

$("#select-from option:not(:selected)").each(function (i, x) {
    alert($(x).val() + " not selected");
});
$('#select-from option').each(function() {
   console.log($(this).text());
});

這將返回所有值。

如果要突出顯示選中的內容,則可以執行if statement來檢查選中的內容,然后使用+將東西合並到輸出文本中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM