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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9619776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 22:59:26  来源:igfitidea点击:

How to create a listbox in HTML without allowing multiple selection?

htmllistbox

提问by CodeBlue

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 -

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

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

But this allows for multiple selection.

但这允许多选。

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.

在这里,提出了一个类似的问题,但“最佳”答案已被否决。所以我不知道还有什么办法可以做到。请帮忙。

回答by aaroncatlin

Just use the size attribute:

只需使用 size 属性:

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

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

澄清一下,添加 size 属性并没有删除多选。

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

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

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

添加 size="5" 属性仍然是一个好主意,这意味着必须至少显示 5 行。在此处查看完整参考

回答by pollirrata

Remove the multiple="multiple" attribute and add SIZE=6 with the number of elements you want

删除 multiple="multiple" 属性并添加 SIZE=6 与您想要的元素数量

you may want to check this site

你可能想看看这个网站

http://www.htmlcodetutorial.com/forms/_SELECT.html

http://www.htmlcodetutorial.com/forms/_SELECT.html

回答by Deshani Tharaka

For Asp.Net MVC

对于 Asp.Net MVC

@Html.ListBox("parameterName", ViewBag.ParameterValueList as MultiSelectList, 
 new { 
 @class = "chosen-select form-control"
 }) 

or

或者

  @Html.ListBoxFor(model => model.parameterName,
  ViewBag.ParameterValueList as MultiSelectList,
   new{
       data_placeholder = "Select Options ",
       @class = "chosen-select form-control"
   })