开发者

HTML select box with a size of 1: when does selectedIndex is -1?

开发者 https://www.devze.com 2022-12-22 15:37 出处:网络
When I use a select box with a size atribute greater than 1 i.e. <select size=\"3\"> <option value=\"a\">a</option>

When I use a select box with a size atribute greater than 1 i.e.

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

If user does not select any item I do get a selectedIndex of -1, as expected.

When i use a 'classic' select box with size="1" the selectedIndex seems to be never -1, if user does not touch the select box, the selectedIndex is 0, that means the 1st item is selected.

This makes sense, but is there a way to show a select box of size="1" with no item selected at the beginning thus that sele开发者_StackOverflow中文版ctedIndex is set to -1.

Thanks!


Sorry, I already answered myself, I simply do this:

<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>
0

精彩评论

暂无评论...
验证码 换一张
取 消