开发者

How to fetch all the option values(selected/unselected) in a selectbox

开发者 https://www.devze.com 2022-12-13 17:04 出处:网络
I want to fetch all the option values(selected/u开发者_如何学Cnselected) in a selectbox on click of a button. How can I do this? I think is a good opportunity to use the Traversing/map method:

I want to fetch all the option values(selected/u开发者_如何学Cnselected) in a selectbox on click of a button. How can I do this?


I think is a good opportunity to use the Traversing/map method:

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

And if you want to get two separate arrays containing the selected and unselected values you can do something like this:

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

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

After that, the values.selected and values.unselected arrays will contain the right elements.


var arr = new Array;

    $("#selectboxid option").each  ( function() {
       arr.push ( $(this).val() );
    });

alert ( arr.join(',' ) );

in the button click

    $("#btn1").click ( function() {
        var arr = new Array;
        $("#selectboxid option").each ( function() {
            arr.push ( $(this).val() );
        });
        alert ( arr );
    });


err ok ..

$('#selectbox').click(function() {
  var allvals = [];
  $(this).find('option').each(function() { allvals.push( $(this).val() ); };
});

or maybe you mean

$('#thebutton').click(function() {
  var allvals = [];
  $('#theselectbox').find('option').each(function() { allvals.push( $(this).val() ); };
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号