如何在不允许多选的情况下在 HTML 中创建列表框?

2022-01-16 00:00:00 listbox html

我在 HTML 方面没有太多经验.我希望创建一个简单的列表框,但要求之一是禁止多选.列表框的大部分代码都是这样的 -

I don't have much experience in HTML. I am looking to create a simple listbox, but one of the requirements is to DISALLOW multiple selection. Most of the code for listboxes goes like this -

 <select name="sometext" multiple="multiple">
    <option>text1</option>
    <option>text2</option>
    <option>text3</option>
    <option>text4</option>
    <option>text5</option>
 </select>

但这允许多选.

这里,有人问了类似的问题,但最佳"答案被否决了.所以我不确定这还能怎么做.请帮忙.

Here, a similar question was asked, but the "best" answer has been downvoted. So I am not sure how else this could be done. Please help.

推荐答案

只要使用size属性即可:

Just use the size attribute:

<select name="sometext" size="5">
  <option>text1</option>
  <option>text2</option>
  <option>text3</option>
  <option>text4</option>
  <option>text5</option>
</select>

为了澄清,添加 size 属性并没有删除多选.

To clarify, adding the size attribute did not remove the multiple selection.

单选有效,因为您删除了 multiple="multiple" 属性.

The single selection works because you removed the multiple="multiple" attribute.

添加 size="5" 属性仍然是个好主意,这意味着至少要显示 5 行.查看完整参考这里

Adding the size="5" attribute is still a good idea, it means that at least 5 lines must be displayed. See the full reference here

相关文章