我如何使第一个选项的选择与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").prop("selectedIndex", 0);
其他回答
// remove "selected" from any options that might already be selected
$('#target option[selected="selected"]').each(
function() {
$(this).removeAttr('selected');
}
);
// mark the first option as selected
$("#target option:first").attr('selected','selected');
$('#newType option:first').prop('selected', true);
使用jQuery和ECMAScript 6检查这个最佳方法:
$('select').each((i, item) => {
var $item = $(item);
$item.val($item.find('option:first').val());
});
如果你要存储select元素的jQuery对象:
var jQuerySelectObject = $("...");
...
jQuerySelectObject.val(jQuerySelectObject.children().eq(0).val());
var firstItem = $(“感兴趣类型”)。(“selectedIndex”道具,0)。瓦尔();
控制台日志(firstItem)。