我如何使第一个选项的选择与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>
当前回答
虽然每个函数可能都不是必需的…
$('select').each(function(){
$(this).find('option:first').prop('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;
});
如果你打算使用第一个选项作为默认的
<select>
<option value="">Please select an option below</option>
...
然后你可以使用:
$('select').val('');
它很漂亮,也很简单。
你可以试试这个
$("#target").prop("selectedIndex", 0);
如果您有禁用选项,您可以添加not([disabled])以防止选择它们,结果如下:
$("#target option:not([disabled]):first").attr('selected','selected')
$('#newType option:first').prop('selected', true);