Make multiple-select to adjust its height to fit options without scroll bar

前端 未结 16 992
说谎
说谎 2020-12-24 03:59

Apparently this doesn\'t work:

   select[multiple]{
     height: 100%;
   }

it makes the select have 100% page height...

auto

相关标签:
16条回答
  • 2020-12-24 04:56

    You can do this with simple javascript...

    <select multiple="multiple" size="return this.length();">
    

    ...or limit height until number-of-records...

    <select multiple="multiple" size="return this.length() > 10 ? this.length(): 10;">
    
    0 讨论(0)
  • 2020-12-24 04:57

    You can only do this in Javascript/JQuery, you can do it with the following JQuery (assuming you've gave your select an id of multiselect):

    $(function () {
        $("#multiSelect").css("height", parseInt($("#multiSelect option").length) * 20);
    });
    

    Demo: http://jsfiddle.net/AZEFU/

    0 讨论(0)
  • 2020-12-24 05:00

    For jQuery you can try this. I always do the following and it works.

    $(function () {
       $("#multiSelect").attr("size",$("#multiSelect option").length);
    });
    
    0 讨论(0)
  • 2020-12-24 05:03

    friends: if you retrieve de data from a DB: you can call this $registers = *_num_rows( Result_query ) then

    <select size=<?=$registers + 1; ?>"> 
    
    0 讨论(0)
提交回复
热议问题