2013-03-28 26 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

不包含空的'