我在 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 中创建列表框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
JavaScript innerHTML 不适用于 IE?JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不适用于 IE?)
NETWORK_ERROR:XMLHttpRequest 异常 101NETWORK_ERROR: XMLHttpRequest Exception 101(NETWORK_ERROR:XMLHttpRequest 异常 101)
如何在 IE 中从 Javascript 访问 XHR responseBody(用于二how do I access XHR responseBody (for binary data) from Javascript in IE?(如何在 IE 中从 Javascript 访问 XHR responseBody(用于二进制数据)
document.write() 覆盖文档?document.write() overwriting the document?(document.write() 覆盖文档?)
AngularJS 错误:跨源请求仅支持协议方案:http、datAngularJS Error: Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, https(AngularJS 错误:跨源请求仅支持
如何获得 XMLHttpRequest 的响应?How to get the response of XMLHttpRequest?(如何获得 XMLHttpRequest 的响应?)