尺寸为1的HTML选择框:selectedIndex什么时候为-1?

发布于 2024-08-24 22:41:49 字数 532 浏览 3 评论 0原文

当我使用尺寸属性大于 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" 的“经典”选择框时,selectedIndex 似乎永远不会为 -1,如果用户不触摸选择框,selectedIndex 为 0,这意味着第一个项目被选中。

这是有道理的,但是有没有办法显示 size="1" 的选择框,并且在开始时没有选择任何项目,从而将 selectedIndex 设置为 -1。

谢谢!

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 selectedIndex is set to -1.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野味少女 2024-08-31 22:41:49

抱歉,我已经回答了自己,我只是这样做:

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文