admin管理员组文章数量:1332604
Using this code:
$('#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>
I get all the values from that select box that are selected, how do I get also the ones not selected?
Using this code:
$('#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>
I get all the values from that select box that are selected, how do I get also the ones not selected?
Share Improve this question asked May 24, 2013 at 9:39 DomingoSLDomingoSL 15.5k25 gold badges104 silver badges179 bronze badges5 Answers
Reset to default 7Try below
get all values
var valuesArray = $("#select-from option").map(function(){
return this.value;
}).get();
get selected and unselected values in different array.
var values = {
selected: [],
unselected:[]
};
$("#select-from option").each(function(){
values[this.selected ? 'selected' : 'unselected'].push(this.value);
});
Thanks,
Siva
This is the code you need:
$('#select-from option').each(function(){
alert($(this).val());
});
Take a look at this jsfiddle
$("#selectId > option").each(function() {
alert(this.text + ' ' + this.value);
});
this.text
will give the text
this.value
will give the value
My version :)
$("#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());
});
This will return all values.
If you want to highlight the selected you could do an if statement
to check if selected and then merge somthing to the output text with a +
.
本文标签: javascriptGet all options from Select box (selected and non selected)Stack Overflow
版权声明:本文标题:javascript - Get all options from Select box (selected and non selected) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742268756a2443903.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论