Bootstrap select dropdown list placeholder

前端 未结 19 1243
谎友^
谎友^ 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:09

    Try @title = "stuff". It worked for me.

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

    I think, the Dropdown box with a class and JQuery code to disable the first option for user to select, will work perfectly as Select Box placeholder.

    <select class="selectboxclass">
       <option value="">- Please Select -</option>
       <option value="IN">India</option>
       <option value="US">America</option>
    </select>
    

    Make the first option disabled by JQuery.

    <script>
    $('select.selectboxclass option:first').attr('disabled', true);
    </script>
    

    This will make the first option of Dropdown as Placeholder and user will no longer able to select the first option.

    Hope It helps!!

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

    Add hidden attribute:

    <select>
        <option value="" selected disabled hidden>Please select</option>
        <option value="">A</option>
        <option value="">B</option>
        <option value="">C</option>
    </select>
    
    0 讨论(0)
  • 2021-01-29 23:11

    Bootstrap select has an noneSelectedText option. You can set it via data-none-selected-text attribute. Documentation.

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

    dropdown or select doesn't have a placeholder because HTML doesn't support it but it's possible to create same effect so it looks the same as other inputs placeholder

        $('select').change(function() {
         if ($(this).children('option:first-child').is(':selected')) {
           $(this).addClass('placeholder');
         } else {
          $(this).removeClass('placeholder');
         }
        });
    .placeholder{color: grey;}
    select option:first-child{color: grey; display: none;}
    select option{color: #555;} // bootstrap default color
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <select class="form-control placeholder">
       <option value="">Your Placeholder Text</option>
       <option value="1">Text 1</option>
       <option value="2">Text 2</option>
       <option value="3">Text 3</option>
    </select>

    if you want to see first option in list remove display property from css

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

    For .html page

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

    for .jsp or any other servlet page.

    <select>
        <option value="" selected="true" disabled="true">Please select</option>
        <option value="">A</option>
        <option value="">B</option>
        <option value="">C</option>
    </select>
    
    0 讨论(0)
提交回复
热议问题