简体   繁体   English

如何设置Select2类型的响应X可编辑表单字段以在Bootstrap 3上正常工作

[英]How to setup responsive X editable form field of type Select2 to work correctly on Bootstrap 3

I need responsive editable form field of type select2 and with the label above it. 我需要类型为select2且其上方带有标签的响应式可编辑表单字段。 Here is my attempt (simplified code) using Bootstrap3, X editable and Select2: 这是我使用Bootstrap3,X editable和Select2的尝试(简化代码):

<form role="form">
    <div class="row">
        <div class="form-group col-sm-4 col-xs-6">
            <label for="gender">Gender</label>
            <a href="#" class="form-control gender" data-type="select2" data-value="M" data-name="gender">Male</a>
        </div>
        <div class="form-group col-sm-4 col-xs-6">
            <label for="gender">Gender2</label>
            <a href="#" class="form-control gender" data-type="select2" data-value="M" data-name="gender">Male</a>
        </div>
    </div>
    <div class="row">
        <div class="form-group col-sm-4 col-xs-6">
            <label for="gender">Gender3</label>
            <a href="#" class="form-control gender" data-type="select2" data-value="M" data-name="gender">Male</a>
        </div>
        <div class="form-group col-sm-4 col-xs-6">
            <label for="gender">Gender4</label>
            <a href="#" class="form-control gender" data-type="select2" data-value="M" data-name="gender">Male</a>
        </div>
        <div class="form-group col-sm-4 col-xs-6">
            <label for="gender">Gender5</label>
            <a href="#" class="form-control gender" data-type="select2" data-value="M" data-name="gender">Male</a>
        </div>
    </div>
</form>

Script 脚本

$(function(){
    $.fn.editable.defaults.mode = 'inline';
    $('#gender').editable({
        showbuttons: false,
        source: [
            {id: 'M', text: 'Male'},
            {id: 'F', text: 'Female'}
        ],
        select2: {
            width: '100%', // THIS DOESN'T WORK AS IT SHOULD
            // hiding search box
            minimumResultsForSearch: -1
        }
    });
});

Here is jsfiddle . 这是jsfiddle

The problem can be seen if you click on editable dropdown (select). 如果单击可编辑下拉菜单(选择),则可以看到问题。

I would like to achieve following behaviour of the select component: 我想实现选择组件的以下行为:

  1. position under label Gender regardless of the display size and regardless of being focused or not 标签性别下的位置,与显示大小,是否聚焦无关
  2. same size regardless of being focused or not 大小相同,无论是否聚焦
  3. responsive to change of the display size when it is focused, just like when it is not focused 像聚焦时一样,对显示尺寸的变化做出响应

In other words, I want focused select component to behave just the same as when it is not focused, to have same position, size and responsiveness. 换句话说,我希望焦点选择组件的行为与未聚焦时的组件相同,并且具有相同的位置,大小和响应能力。 Now it does maintain height, thanks to added css, but it doesn't have the same position, and it is not responsive, as when it is not focused. 现在,由于添加了css,它的确保持了高度,但是它没有相同的位置,并且没有响应,就像没有聚焦时一样。

How can I achieve this? 我该如何实现?

Edited 编辑

If you want the select in your fiddle to behave responsively, then you have to remove the form-inline class. 如果您希望小提琴中的选择具有响应性,则必须删除form-inline类。 That class is specifically designed not to honor the typical form-control width:100% and enable your elements to float next to one another "inline". 该类专门设计为不遵守典型的表单控件width:100%并使元素彼此“内联”浮动。

The doc says that select2 elements will try to honor css in stylesheets but recommends inline style so this is what works for me: 该文档说select2元素将尝试在样式表中使用css,但建议使用内联样式,因此这对我有效:

HTML: HTML:

<div class="form-group">
    <label for="gender">Gender</label>
    <input type="hidden" id="gender" style="width:100%">
</div>

JS: JS:

$(function(){
  $('#gender').select2({
    placeholder: 'Gender',
    data: [{id: 'M', text: 'Male'},{id: 'F', text: 'Female'}],
    minimumResultsForSearch: -1
  });
});

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

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