我如何使第一个选项的选择与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');
其他回答
另一种重置值(对于多个选定元素)的方法是:
$("selector").each(function(){
/*Perform any check and validation if needed for each item */
/*Use "this" to handle the element in javascript or "$(this)" to handle the element with jquery */
this.selectedIndex=0;
});
对我来说,只有当我添加下面这行代码时,它才能工作
$('#target').val($('#target').find("option:first").val());
$('#newType option:first').prop('selected', true);
$("#target").val($("#target option:first").val());
你可以试试这个
$("#target").prop("selectedIndex", 0);