2011-10-31 109 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)