Bootstrap select dropdown list placeholder

前端 未结 19 1244
谎友^
谎友^ 2021-01-29 22:39

I am trying to make a dropdown list that contains a placeholder. It doesn\'t seem to support placeholder=\"stuff\" as other forms do. Is there a different way to ob

相关标签:
19条回答
  • 2021-01-29 23:18

    If you are initializing the select field through javascript, the following can be added to replace the default placeholder text

    noneSelectedText: 'Insert Placeholder text'

    example: if you have:

    <select class='picker'></select>
    

    in your javascript, you initialize the selectpicker like this

    $('.picker').selectpicker({noneSelectedText: 'Insert Placeholder text'});  
    
    0 讨论(0)
  • 2021-01-29 23:18

    Using Bootstrap, This is what you can do. Using class "text-hide", the disabled option will be shown at first but not on the list.

    <select class="form-control" >
      <option selected disabled class="text-hide">Title</option>
      <option>A</option>
      <option>B</option>
      <option>C</option>
    </select>
    
    0 讨论(0)
  • 2021-01-29 23:19

    Use hidden:

    <select>
        <option hidden >Display but don't show in list</option>
        <option> text 1 </option>
        <option> text 2 </option>
        <option> text 3 </option>
    </select>
    
    0 讨论(0)
  • 2021-01-29 23:19

    Try this:

    <select class="form-control" required>
    <option value="" selected hidden>Select...</option>
    

    when using required + value="" then user can not select it using hidden will make it hidden from the options list, when the user open the options box

    0 讨论(0)
  • 2021-01-29 23:20

    Yes just "selected disabled" in the option.

    <select>
        <option value="" selected disabled>Please select</option>
        <option value="">A</option>
        <option value="">B</option>
        <option value="">C</option>
    </select>
    

    Link to fiddle

    You can also view the answer at

    https://stackoverflow.com/a/5859221/1225125

    0 讨论(0)
  • 2021-01-29 23:20

    Here's another way to do it

    <select name="GROUPINGS[xxxxxx]" style="width: 60%;" required>
        <option value="">Choose Platform</option>
    <option value="iOS">iOS</option>
        <option value="Android">Android</option>
        <option value="Windows">Windows</option>
    </select>
    

    "Choose Platform" becomes the placeholder and the 'required' property ensures that the user has to select one of the options.

    Very useful, when you don't want to user field names or Labels.

    0 讨论(0)
提交回复
热议问题