简体   繁体   English

如果我的html文件中有多个select2,如何更改特定select2的宽度?

[英]how to change the width of a specific select2 if i am having more than 1 select2 in my html file?

I am having more than 1 select2 in my HTML file. 我的HTML文件中有1个以上的select2。 I want to set a width to a specific select2 and a common width to all the other select2. 我想为特定的select2设置一个宽度,为所有其他select2设置一个公共宽度。 The common width is affecting the width of all the select and even though if I am trying to override the width through jquery it is not happening. 通用宽度会影响所有选择的宽度,即使我试图通过jquery覆盖该宽度也不会发生。

CSS CSS

.select2-container {
    width: 100% !important;
}

HTML HTML

<div class="col-md-12">
    <select class="select2 form-control m-t-15"  id="user_name">
        <option selected="selected">Select Name</option>
    </select>
    <select class="select2 form-control m-t-15"  id="user_roll">
        <option selected="selected">Select Roll</option>
    </select>
    <select class="select2 form-control m-t-15"  id="user_address">
        <option selected="selected">Select Address</option>
    </select>
</div>

JQUERY JQUERY

    $(".select2").select2();
    $("#user_address").select2({ width: '50%' });

Expected Result:- select 2 of id "user_address" should be having width 50%. 预期结果:-选择ID为“ user_address”的2个应具有50%的宽度。

Current Result:- All the select 2 is having width 100% 当前结果:-所有选择2的宽度均为100%

Anybody any idea how to set 50% width to select2 of id "user_address"? 有人知道如何设置ID为“ user_address”的select2的50%宽度吗?

You already have id's for each select2 therefore u can style that specific select2 with its id for ex let's style 'user_roll': 您已经为每个select2设置了ID,因此您可以为其ID设置特定的select2的样式,例如ex's style'user_roll':

#user_roll{
//your styling here, noting that "#" is for the id just like the "." is for class
  }

Instead of setting option as selected use placeholder in select2 而不是将选项设置为选定,而是在select2中使用占位符

 $(".select2").select2({ width: '100%',placeholder:'Choose an Option', allowClear: true }); $("#user_address").select2({ width: '60%' ,placeholder:'Choose an Address', allowClear: true }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.10/js/select2.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.10/css/select2.min.css" rel="stylesheet"/> <div class="col-md-12"> <select class="select2 form-control mt-15" id="user_name"> </select> <select class="select2 form-control mt-15" id="user_roll"> </select> <select class="select2 form-control mt-15" id="user_address"> </select> </div> 

just changes in css above your css code please remove it and replace with below code : 只是您的CSS代码上方的CSS发生了变化,请将其删除并替换为以下代码:

.select2 {
 width: 100%;
}

Try this: 尝试这个:

#user_address{
  width: 50%;
}

try this 尝试这个

 .select-roll-width{ width:50%; } 
 <div class="col-md-12"> <select class="select2 form-control mt-15" id="user_name"> <option selected="selected">Select Name</option> </select> <select class="select2 select-roll-width form-control mt-15" id="user_roll"> <option selected="selected">Select Roll</option> </select> <select class="select2 form-control mt-15" id="user_address"> <option selected="selected">Select Address</option> </select> </div> 

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

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