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 中创建列表框?


- Fetch API 如何获取响应体? 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 失败的 Canvas 360 jquery 插件 2022-01-01