我如何使第一个选项的选择与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>
<option value="">Please select an option below</option>
...
然后你可以使用:
$('select').val('');
它很漂亮,也很简单。
其他回答
以下是我的做法:
$("#target option")
.removeAttr('selected')
.find(':first') // You can also use .find('[value=MyVal]')
.attr('selected','selected');
它只适用于我在最后使用触发器('change'),就像这样:
$("#target option:first").attr('selected','selected').trigger('change');
另一种重置值(对于多个选定元素)的方法是:
$("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;
});
var firstItem = $(“感兴趣类型”)。(“selectedIndex”道具,0)。瓦尔();
控制台日志(firstItem)。
$("#target")[0].selectedIndex = 0;