获取选择框的默认名称

时间:2011-10-31 22:44:44

标签: jquery

我正在尝试使用jquery找到哪个选择框设置为默认值。

我有这么多设置。

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

这给了我每个选择框的默认值,但我试图找到它属于哪个选项。

2 个答案:

答案 0 :(得分:0)

应该是......

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

答案 1 :(得分: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)

相关问题