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

Using this code:$('#select-from').each(function() {alert($(this).val());});<select name=&

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742268756a4412297.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信