简体   繁体   English

jQuery的控制高度选择下拉列表

[英]Control Height of jQuery Chosen Drop Down List

I am trying to control the displayed height of the jQuery Chosen drop down.我正在尝试控制 jQuery Chosen 下拉菜单的显示高度。 The following code does not seem to have an affect on the height.以下代码似乎对高度没有影响。

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<div class="hide">
    <INPUT type="text" class="namecontrol" size="1" value="${employeeMasterParmsObj.controller}">
</div>

    <select class="usernamelist">
        <option value="0">Select from List</option>
        <c:forEach var="itemName" varStatus="status" items="${employeeMasterListObj.employeeMasterList}">
            <option value="${itemName.badge}-${itemName.fname} ${itemName.lname}">${itemName.fname}&nbsp;&nbsp;${itemName.lname}&nbsp;&nbsp;${itemName.badge}-${itemName.jobclassdescp}</option>
        </c:forEach>
    </select>

<script type="text/javascript">
$(document).ready(function(){

    $('.usernamelist').chosen({ width: "400px"});
    $(".chosen-results").css({'font-size':'10px', 'max-height':'150px !important'});
    $(".chosen-container").css({'font-size':'10px', 'max-height':'150px !important'});
                    
    $('.usernamelist').on("change", function(){
        if(this.value != "0"){
            putUserName(this.value);
        }
    });
});

}

Why don't you simply use CSS to do that?你为什么不简单地使用 CSS 来做到这一点?

.chosen-container .chosen-results {
    max-height: 150px !important;
}

UPDATE:更新:

To use it for different sizes add multiple custom classes:要将其用于不同的尺寸,请添加多个自定义类:

.chosen-small .chosen-container .chosen-results {
    max-height: 150px !important;
}
.chosen-medium .chosen-container .chosen-results {
    max-height: 250px !important;
}
.chosen-large .chosen-container .chosen-results {
    max-height: 350px !important;
}

The only problem with your current code is that you forgot the following (see css ):当前代码的唯一问题是您忘记了以下内容(请参阅css ):

Note: .css() ignores !important declarations.注意: .css()忽略!important声明。 So, the statement $( "p" ).css( "color", "red !important" ) does not turn the color of all paragraphs in the page to red.因此,语句$( "p" ).css( "color", "red !important" )不会将页面中所有段落的颜色变为红色。 It's strongly advised to use classes instead;强烈建议改用类; otherwise use a jQuery plugin.否则使用 jQuery 插件。

Currently only the font-size is applied and max-height is ignored.当前仅应用font-size并忽略max-height Your code should work if you remove the !important bit.如果您删除!important位,您的代码应该可以工作。

 $('.usernamelist').chosen({ width: "400px"}); $(".chosen-results").css({'font-size':'10px', 'max-height':'150px'}); $(".chosen-container").css({'font-size':'10px', 'max-height':'150px'});
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.min.css" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.jquery.min.js"></script> <select class="usernamelist"> <option value="0">Select from List</option> <option value="1">Aphrodite</option> <option value="2">Apollo</option> <option value="3">Ares</option> <option value="4">Artemis</option> <option value="5">Athena</option> <option value="6">Hades</option> <option value="7">Hephaestus</option> <option value="8">Hera</option> <option value="9">Hermes</option> <option value="10">Hestia</option> <option value="11">Poseidon</option> <option value="12">Zeus</option> </select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM