2009-12-04 140 views

回答

10
var arr = new Array; 

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

alert (arr.join(',')); 
在按鈕

點擊

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

ERR確定..

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

或者你的意思是

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

我認爲是使用Traversing/map的好機會方法:

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

如果你想獲得包含選定和未選定值,你可以做這樣的事情兩個獨立的數組:

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

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

之後,values.selectedvalues.unselected陣列將包含正確的元素。

相關問題