2011-10-31 110 views
1

我試圖找到哪個選擇框設置爲默認使用jQuery。獲取選擇框的默認名稱

我有這麼多的設置。

var selectBoxes = $('select', this.el); 
selectBoxes.each(function(){ 
    console.log("val = ", $(this).val()); 
}); 

這給了我每個選擇框的默認值,但我試圖找到它屬於哪個選項。

+0

使用'select [i] .selectedIndex'和'select [i] .options [select [i] .selectedIndex]'來獲得選項。 –

+0

謝謝Rob,那個解決了我之後的事情。 – Chapsterj

回答

0

應該..

var selectBoxes = $('select', this.el); 
selectBoxes.children().each(function(){ 
    console.log($(this).text()); 
}); 
0

闡述我在OP的評論答案:

//Select every `<select>` which is a child of this.el, and loop through it 
var selectBoxes = $('select', this.el); 
selectBoxes.each(function(){ 
    //`this` points to the select element 
    // The following declaration will define a HTMLOptionElement 
    var selectedOption = select.options[this.selectedIndex]; 
    var value = selectedOption.value;  //Recommended 
    //Alternatively: $(selectedOption).val(); 
}); 

如果你想使用選擇的選項元素jQuery方法,包對象在一個jQuery構造函數:$(selectedOption)