我如何使第一个选项的选择与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>
当前回答
另一种重置值(对于多个选定元素)的方法是:
$("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;
});
其他回答
您可以使用它从下拉菜单中选择任何选项。
// 'N' can by any number of option. // e.g., N=1 for first option
$("#DropDownId").val($("#DropDownId option:eq(N-1)").val());
就像这样简单:
$('#target option:first').prop('selected', true);
如果你要存储select元素的jQuery对象:
var jQuerySelectObject = $("...");
...
jQuerySelectObject.val(jQuerySelectObject.children().eq(0).val());
如果你打算使用第一个选项作为默认的
<select>
<option value="">Please select an option below</option>
...
然后你可以使用:
$('select').val('');
它很漂亮,也很简单。
对我来说,只有当我添加下面这行代码时,它才能工作
$('#target').val($('#target').find("option:first").val());