2011-06-20 111 views
0

我得到一个代码:jQuery选择别无选择

$('select :not(option[value])').attr('selected', 'selected'); 

HTML:

<select> 
<option>A</option> 
<option value=''>B</option> 
<option value='1'>C</option> 
</select> 

现在我要选择没有价值属性选项A.选项。但我的jQuery代码不起作用。任何想法使用选择器?

+0

什么是你想在这里做什么?您想将第一个选项设置为选中状态,但是由于您没有明确选择设置,因此默认情况下它不会始终处于选中状态? – kinakuta

回答

4

:not()只需要选择器。从here你可以看到属性中[square brackets]定义,因为你想要的options没有valueattr,这应该工作:

$('option:not([value])').attr('selected', 'selected');