問(wèn)題描述
我在 HTML 方面沒(méi)有太多經(jīng)驗(yàn).我希望創(chuàng)建一個(gè)簡(jiǎn)單的列表框,但要求之一是禁止多選.列表框的大部分代碼都是這樣的 -
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>
但這允許多選.
這里,有人問(wèn)了類似的問(wèn)題,但最佳"答案被否決了.所以我不確定這還能怎么做.請(qǐng)幫忙.
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 屬性并沒(méi)有刪除多選.
To clarify, adding the size attribute did not remove the multiple selection.
單選有效,因?yàn)槟鷦h除了 multiple="multiple" 屬性.
The single selection works because you removed the multiple="multiple" attribute.
添加 size="5" 屬性仍然是個(gè)好主意,這意味著至少要顯示 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
這篇關(guān)于如何在不允許多選的情況下在 HTML 中創(chuàng)建列表框?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!