2010-03-12 67 views
0

當我使用一個大小選擇框屬性附加傷害大於1,即大小爲1的HTML選擇框:when selectedIndex是否爲-1?

<select size="3"> 
    <option value="a">a</option> 
    <option value="b">b</option> 
    <option value="c">c</option> 
    <option value="d">d</option> 
</select> 

如果用戶不選擇我做任何項目獲得-1的selectedIndex,符合市場預期。

當我使用size =「1」的'classic'選擇框時,selectedIndex似乎永遠不會是-1,如果用戶沒有碰到選擇框,selectedIndex是0,這意味着第一項被選中。

這是有道理的,但有沒有辦法顯示大小=「1」的選擇框,在開始時沒有選擇項目,因此selectedIndex被設置爲-1。

謝謝!

回答

0

對不起,我已經回答了我自己,我只是這樣做:

<select id="test" name="test" size="1"> 
    <option value="a">a</option> 
    <option value="b">b</option> 
    <option value="c">c</option> 
    <option value="d">d</option> 
</select> 
<script type="text/javascript"> 
    //this makes the select box to be displaied with NO item selected 
    document.getElementById('test').selectedIndex = -1; 
</script>