2013-03-28 50 views
1

是否有可能是有selectedIndex返回-1如果没有被选中,而不是元素的文字位置0返回的SelectedIndex从选择元素

另外,selectedIndex返回0,即使没有被选中。

<select id="qwe" name="qwe"> 
    <option>11</option> 
    <option>22</option> 
</select> 

document.someForm.qwe.value 
//returns 11 

$('#qwe option:selected').val() 
//returns 11 



<select id="qwe" name="qwe"> 
    <option selected="false">11</option> 
    <option>22</option> 
</select> 
$('#qwe option:selected').val() 
//returns 11 

<select id="qwe" name="qwe"> 
    <option selected="false">11</option> 
    <option selected="false">22</option> 
</select> 
$('#qwe option:selected').val() 
//returns 22 
+0

添加到列表中。 – Derek

回答

-1

选择框必须始终选择一个值(默认为第一个选项)。如果你想要这样的行为,只需在你的列表顶部添加一个空选项,并使用任何你想要的值(例如-1)。

+0

thx我认为这将是最简单的方式 –

+0

不包含空的'