jQuery - 将select的值设置为其中一个选项

Ale*_*lex 5 html javascript forms jquery

如何将选择输入字段的值设置为未禁用的第一个选项的值

例如,在此字段中,所选值为1.如何将此值更改为下一个非禁用选项,即.2?

<select>
  <option disabled="disabled" value="1">1</option
  <option value="2">2</option
  <option disabled="disabled" value="3">3</option
  <option value="4">4</option
</select>
Run Code Online (Sandbox Code Playgroud)

小智 13

对于你给出的例子,我会使用:

$("select option:not([disabled])").first().attr("selected", "selected");

如果你的select有一个特定的ID(例如"#foo"):

$("#foo option:not([disabled])").first().attr("selected", "selected");


mpe*_*pen 5

比其他解决方案更清洁:

$('#select_id').find('option:enabled:first').prop('selected',true);
Run Code Online (Sandbox Code Playgroud)