Bootstrap select dropdown list placeholder

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

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

提交回复
热议问题