最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get all options from Select box (selected and non selected) - Stack Overflow

programmeradmin4浏览0评论

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 badges
Add a ment  | 

5 Answers 5

Reset to default 7

Try 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 +.

发布评论

评论列表(0)

  1. 暂无评论