使用Bootstrap创建响应式输入表单

时间:2013-04-10 11:20:38

标签: twitter-bootstrap responsive-design

enter image description here


我正在尝试实现屏幕截图中显示的屏幕。但我无法控制元素之间的空间。我希望它能够响应,但我想我这次无法管理高分辨率屏幕布局。代码如下,我添加了屏幕的线框。 在添加一些前置元素时,它总是破坏我的deams:)

链接到文件:jsfiddle.net/AmhHf

![<div class="container-fluid">
  <div class="row-fluid">
    <div class="span8">
      <h1>Formulate</h1>
    </div>
    <div class="span4 pull-right">&nbsp;</div>
  </div>
  <div class="row-fluid">
    <form class="well span12">
      <div class="row-fluid">
        <div class="span12">
          <label>Formula Name</label>
          <input type="text" class="span8"> 
        </div>
        <div class="span8">&nbsp;</div>
      </div>
      <div class="row-fluid">
        <div class="span3">
          <label>My Label</label>
        </div>
        <div class="span3">
          <div class="input-prepend">
            <span class="add-on">@</span>
            <input type="text" class="input-medium"> 
          </div>
        </div>
        <div class="span3">+</div>
        <div class="span3">
          <select>
            <option>Option 1</option>
            <option>Option 2</option>
            <option>Option 3</option>
          </select>
        </div>
      </div>
      <div class="row-fluid">
        <div class="span3">
          <label>My Label</label>
        </div>
        <div class="span3">
          <div class="input-prepend">
            <span class="add-on">@</span>
            <input type="text" class="input-medium"> 
          </div>
        </div>
        <div class="span3">+</div>
        <div class="span3">
          <select>
            <option>Option 1</option>
            <option>Option 2</option>
            <option>Option 3</option>
          </select>
        </div>
      </div>
      <div class="row-fluid">
        <div class="span3">
          <label>My Label</label>
        </div>
        <div class="span3">
          <div class="input-prepend">
            <span class="add-on">@</span>
            <input type="text" class="input-medium"> 
          </div>
        </div>
        <div class="span3">+</div>
        <div class="span3">
          <select>
            <option>Option 1</option>
            <option>Option 2</option>
            <option>Option 3</option>
          </select>
        </div>
      </div>
      <div class="row-fluid"></div>
      <div class="row-fluid">
        <div class="span3">
          <label>My Label</label>
        </div>
        <div class="span3">
          <div class="input-prepend">
            <span class="add-on">@</span>
            <input type="text" class="input-medium"> 
          </div>
        </div>
        <div class="span3">+</div>
        <div class="span3">
          <select>
            <option>Option 1</option>
            <option>Option 2</option>
            <option>Option 3</option>
          </select>
        </div>
      </div>
      <div class="row-fluid">
        <div class="form-actions pull-right">
          <button type="submit" class="btn btn-primary">Submit</button>
          <input type="reset" class="btn" value="Reset"> 
        </div>
      </div>
    </form>
  </div>
</div>][3] 

编辑:

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pagetitle</title>
<link href="css/bootstrap.css" rel="stylesheet" type="text/css">
<link href="css/bootstrap-responsive.css" rel="stylesheet" type="text/css">
<!-- HTML5 shim for IE backwards compatibility -->
<!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
</head>

1 个答案:

答案 0 :(得分:2)

这似乎与将在v3中解决的a bug in bootstrap相关。

根据该链接中的一条评论,您现在可以使用此jQuery代码段来解决此问题:

jQuery(function($) {
  $(window).resize(function() {
    $(".input-prepend, .input-append").each(function(index, group) {
      var input = $("input", group).css('width', '');
      $(".add-on, .btn", group).each(function() {
        input.css('width', '-=' + $(this).outerWidth());
      });
    });
  }).trigger('resize');
});

我建议您将输入的类更改为input-block-level

<div class="row-fluid">
  <div class="span3">
    <label>My Label</label>
  </div>
  <div class="span3">
    <div class="input-prepend">
        <span class="add-on">@</span>
        <input type="text" class="input-block-level"/> 
    </div>
  </div>
  <div class="span3">+</div>
  <div class="span3">
    <select class="input-block-level">
      <option>Option 1</option>
      <option>Option 2</option>
      <option>Option 3</option>
    </select>
  </div>
</div>

请参阅此updated jsfiddle以及第一行输入现在如何正确调整大小。