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

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

当前回答

Use:

alert($( "#target option:first" ).text());

其他回答

$("#target").val($("#target option:first").val());
// 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');

如果你要存储select元素的jQuery对象:

var jQuerySelectObject = $("...");

...

jQuerySelectObject.val(jQuerySelectObject.children().eq(0).val());

它只适用于我在最后使用触发器('change'),就像这样:

$("#target option:first").attr('selected','selected').trigger('change');

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

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

然后你可以使用:

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

它很漂亮,也很简单。