我知道我可以做设置选择指数
$('#selectBox :nth-child(4)').attr('selected', 'selected');
选择在选择框中选择一个选项,但我怎么能做到这一点,通过返回的jQuery对象?例如像
var box = $('#selectBox');
$(box :nth-child(4)').attr('selected', 'selected');
我知道我可以做设置选择指数
$('#selectBox :nth-child(4)').attr('selected', 'selected');
选择在选择框中选择一个选项,但我怎么能做到这一点,通过返回的jQuery对象?例如像
var box = $('#selectBox');
$(box :nth-child(4)').attr('selected', 'selected');
您可以用类似环境中使用的选择:
$(':nth-child(4)', box).attr('selected', 'selected');
PS:如果您知道选项值,则可以使用.val()
方法。
$('#selectBox').val(the_value);
你可能想使用prop()为。
var box = $('#selectBox');
box.children(':nth-child(4)').prop('selected', 'selected');
的可能重复的[jQuery的:在选择一个元件已被选择的元素中](http://stackoverflow.com/questions/629730/jquery-selecting-an-element-in-inside-an-already - 选择元素) –