<SELECT multiple> - how to allow only one item selected?

后端 未结 8 1053
遇见更好的自我
遇见更好的自我 2021-01-29 23:44

I have a

提交评论

  • 2021-01-30 00:20

    If the user should select only one option at once, just remove the "multiple" - make a normal select:

      <select name="mySelect" size="3">
         <option>Foo</option>
         <option>Bar</option>
         <option>Foo Bar</option>
         <option>Bar Foo</option>
      </select>
    

    Fiddle

    0 讨论(0)
  • 2021-01-30 00:24

    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.

    See this: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select_multiple

    Can you please clarify your question?

    0 讨论(0)
  • 2021-01-30 00:26
    <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.

    0 讨论(0)
  • 2021-01-30 00:30

    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.

    Use the tools you have, that's what they're for.

    0 讨论(0)
  • 2021-01-30 00:33

    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
    });
    
    0 讨论(0)
  • 提交回复
    热议问题