我如何使第一个选项的选择与jQuery?
<select id="target">
<option value="1">...</option>
<option value="2">...</option>
</select>
我如何使第一个选项的选择与jQuery?
<select id="target">
<option value="1">...</option>
<option value="2">...</option>
</select>
当前回答
以下是我的做法:
$("#target option")
.removeAttr('selected')
.find(':first') // You can also use .find('[value=MyVal]')
.attr('selected','selected');
其他回答
你可以试试这个
$("#target").prop("selectedIndex", 0);
虽然每个函数可能都不是必需的…
$('select').each(function(){
$(this).find('option:first').prop('selected', 'selected');
});
对我有用。
这对我很管用!
$("#target").prop("selectedIndex", 0);
对我来说,只有当我添加下面这行代码时,它才能工作
$('#target').val($('#target').find("option:first").val());
就像这样简单:
$('#target option:first').prop('selected', true);