Html <SELECT multiple> - 如何只允许选择一项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5338229/
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
<SELECT multiple> - how to allow only one item selected?
提问by simPod
I have a <SELECT multiple>
field with multiple options and I want to allow it to have only one option selected at the same time but user can hold CTRL key and select more items at once.
我有一个<SELECT multiple>
包含多个选项的字段,我希望允许它同时只选择一个选项,但用户可以按住 CTRL 键并一次选择更多项目。
Is there any way how to do it? (I don't want to remove 'multiple').
有什么办法可以做到吗?(我不想删除“多个”)。
回答by Marcos Placona
Just don't make it a select multiple, but set a size to it, such as:
只是不要让它成为一个多选,而要为其设置一个大小,例如:
<select name="user" id="userID" size="3">
<option>John</option>
<option>Paul</option>
<option>Ringo</option>
<option>George</option>
</select>
Working example: https://jsfiddle.net/q2vo8nge/
回答by MacGucky
回答by David
Why don't you want to remove the multiple
attribute? The entire purpose of that attribute is to specify to the browser that multiple values may be selected from the given select
element. If only a single value should be selected, remove the attribute and the browser will know to allow only a single selection.
你为什么不想删除该multiple
属性?该属性的全部目的是向浏览器指定可以从给定select
元素中选择多个值。如果只应选择一个值,则删除该属性,浏览器将知道只允许进行一次选择。
Use the tools you have, that's what they're for.
使用您拥有的工具,这就是它们的用途。
回答by Cogicero
You want only one option by default, but the user can select multiple options by pressing the CTRL key. This is (already) exactly how the SELECT multiple is meant to behave.
默认情况下您只需要一个选项,但用户可以通过按 CTRL 键选择多个选项。这(已经)正是 SELECT 倍数的行为方式。
See this: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select_multiple
看到这个:http: //www.w3schools.com/tags/tryit.asp?filename=tryhtml_select_multiple
Can you please clarify your question?
你能澄清一下你的问题吗?
回答by Ajay2707
I am coming here after searching google and change thing at my-end.
我在搜索谷歌后来到这里并在我的最后改变了事情。
So I just change this sample and it will work with jquery at run-time.
所以我只是更改了这个示例,它可以在运行时与 jquery 一起使用。
$('select[name*="homepage_select"]').removeAttr('multiple')
回答by Ewan
I had some dealings with the select \ multi-select this is what did the trick for me
我与 select \ multi-select 打交道,这对我有用
<select name="mySelect" multiple="multiple">
<option>Foo</option>
<option>Bar</option>
<option>Foo Bar</option>
<option>Bar Foo</option>
</select>
回答by Ste
<select name="flowers" size="5" style="height:200px">
<option value="1">Rose</option>
<option value="2">Tulip</option>
</select>
This simple solution allows to obtain visually a list of options, but to be able to select only one.
这个简单的解决方案允许在视觉上获得一个选项列表,但只能选择一个。
回答by DotNetFullStack
Late to answer but might help someone else, here is how to do it without removing the 'multiple' attribute.
回答晚了但可能会帮助其他人,这里是如何在不删除“多个”属性的情况下做到这一点。
$('.myDropdown').chosen({
//Here you can change the value of the maximum allowed options
max_selected_options: 1
});