How to create a listbox in HTML without allowing multiple selection?(如何在不允许多选的情况下在 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
这篇关于如何在不允许多选的情况下在 HTML 中创建列表框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在不允许多选的情况下在 HTML 中创建列表框?


基础教程推荐
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01