我如何使第一个选项的选择与jQuery?

<select id="target">
  <option value="1">...</option>
  <option value="2">...</option>
</select>

当前回答

$("#target")[0].selectedIndex = 0;

其他回答

$("#target")[0].selectedIndex = 0;

如果你打算使用第一个选项作为默认的

<select>
    <option value="">Please select an option below</option>
    ...

然后你可以使用:

$('select').val('');

它很漂亮,也很简单。

我认为,关于投票最多的答案,我发现了一个微妙的问题:即使它们正确地更改了所选的值,它们也不会更新用户看到的元素(只有当用户单击小部件时,才会在更新的元素旁边看到一个复选框)。

将.change()调用链接到末尾也会更新UI小部件。

$("#target").val($("#target option:first").val()).change();

(注意,我是在使用jQuery Mobile和Chrome桌面框时注意到这一点的,所以这可能不是到处都是情况)。

虽然每个函数可能都不是必需的…

$('select').each(function(){
    $(this).find('option:first').prop('selected', 'selected');
});

对我有用。

就像这样简单:

$('#target option:first').prop('selected', true);