如何在选择框中获取所有选项值(选中/未选中)

kay*_*een 7 jquery drop-down-menu

我想在单击按钮时在选择框中获取所有选项值(选中/未选中).我怎样才能做到这一点?

CMS*_*CMS 15

我认为这是一个使用Traversing/map方法的好机会:

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

如果你想得到两个包含所选和未选择值的独立数组,你可以这样做:

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

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

之后,values.selectedvalues.unselected数组将包含正确的元素.


rah*_*hul 10

var arr = new Array;

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

alert ( arr.join(',' ) );
Run Code Online (Sandbox Code Playgroud)

在按钮单击中

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