Che*_*eng 31 html javascript jquery
我有以下HTML
<select onchange="this.form.submit()" name="name">
<option value="9995101E01#17201044055PM">9995101E01#17201044055PM</option>
<option value="GRR-Example">GRR-Example</option>
<option value="admin">admin</option>
<option value="123w">123w</option>
</select>
Run Code Online (Sandbox Code Playgroud)
我可以知道如何使用JQuery来选择值"admin"吗?
Dar*_*rov 88
$('select[name=name] option:eq(2)').attr('selected', 'selected');
Run Code Online (Sandbox Code Playgroud)
演示.
Ale*_*lex 11
像这样:
$("select[name='name'] option:eq(2)").attr("selected", "selected");
Run Code Online (Sandbox Code Playgroud)
编辑:
要在新编辑中执行所需操作,即选择具有以下值的选项admin:
$("select[name='name'] option:contains('admin')").attr("selected", "selected");
Run Code Online (Sandbox Code Playgroud)
看它工作.
小智 7
我认为nth-child伪类是你正在寻找的:
$('select option:nth-child(3)').attr('selected', 'selected');
Run Code Online (Sandbox Code Playgroud)