[英]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);
});
謝謝,
西瓦
$("#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.