Bootstrap select dropdown list placeholder

前端 未结 19 1245
谎友^
谎友^ 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:25
    <option value="" defaultValue disabled> Something </option>
    

    you can replace defaultValue with selected but that would give warning.

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

    All this options are.... improvisations. Bootstrap already offers a solution for this. If you want to change the placeholder for all your dropdown elements you can change the value of

    noneSelectedText in the bootstrap file.

    To change individualy, you can use TITLE parameter.

    example:

    <div class="form-group">
              <label class="control-label col-xs-2" id="country">Continent:</label>
              <div class="col-xs-9">
                <select name="zones[]" class="selectpicker" title="All continents" id="zones" multiple >
                  <option value="Africa">Africa</option>
                  <option value="Asia">Asia</option>
                  <option value="North America">America North</option>
                  <option value="South America">America South</option>
                  <option value="Antarctica">Antarctica</option>
                  <option value="Australia">Australia</option>
                  <option value="Europe">Europe</option>
                </select>
              </div>
            </div>
    
    0 讨论(0)
  • 2021-01-29 23:31

    Most of the options are problematic for multi-select. Place Title attribute, and make first option as data-hidden="true"

    <select class="selectpicker" title="Some placeholder text...">
        <option data-hidden="true"></option>
        <option>First</option>
        <option>Second</option>
    </select>
    
    0 讨论(0)
  • 2021-01-29 23:33

    This is for Bootstrap 4.0, you only need to enter selected on the first line, it acts as a placeholder. The values are not necessary, but if you want to add value 0-... that is up to you. Much simpler than you may think:

    <select class="custom-select">
       <option selected>Open This</option>
       <option value="">1st Choice</option>
       <option value="">2nd Choice</option>
    </select>
    

    This is link will guide you for further information.

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

    Using bootstrap, if you need to also add some values to the option to use for filters or other stuff you can simply add the class "bs-title-option" to the option that you want as a placeholder:

    <select class="form-group">
       <option class="bs-title-option" value="myVal">My PlaceHolder</option>
       <option>A</option>
       <option>B</option>
       <option>c</option>
    </select>
    

    Bootstrap adds this class to the title attribute.

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

    Solution for Angular 2

    Create a label on top of the select

    <label class="hidden-label" for="IsActive"
        *ngIf="filterIsActive == undefined">Placeholder text</label>
    <select class="form-control form-control-sm" type="text" name="filterIsActive"
        [(ngModel)]="filterIsActive" id="IsActive">
        <option value="true">true</option>
        <option value="false">false</option>
    </select>
    

    and apply CSS to place it on top

    .hidden-label {
        position: absolute;
        margin-top: .34rem;
        margin-left: .56rem;
        font-style: italic;
        pointer-events: none;
    }
    

    pointer-events: none allows you to display the select when you click on the label, which is hidden when you select an option.

    angular html css

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