Bootstrap输入框组

前面的话

  有时,我们需要将文本输入框(input group)和文件或者小icon组合在一起进行显示, 我们称之为addon。也就是通过在文本输入框 <input> 前面、后面或是两边加上文字或按钮,来实现对表单控件的扩展。本文将详细介绍Bootstrap输入框组

基本用法

  在输入框的任意一侧添加额外元素或按钮。还可以在输入框的两侧同时添加额外元素

<div class="input-group">
<span class="input-group-addon" id="basic-addon1">@</span>
<input type="text" class="form-control" placeholder="Username" aria-describedby="basic-addon1">
</div> <div class="input-group">
<input type="text" class="form-control" placeholder="Recipient's username" aria-describedby="basic-addon2">
<span class="input-group-addon" id="basic-addon2">@example.com</span>
</div> <div class="input-group">
<span class="input-group-addon">$</span>
<input type="text" class="form-control" aria-label="Amount (to the nearest dollar)">
<span class="input-group-addon">.00</span>
</div> <div class="input-group">
<span class="input-group-addon" id="basic-addon3">https://example.com/users/</span>
<input type="text" class="form-control" id="basic-url" aria-describedby="basic-addon3">
</div>

【注意事项】

  1、避免使用 <select> 元素,因为 WebKit 浏览器不能完全绘制它的样式

  2、避免使用 <textarea> 元素,由于它们的 rows 属性在某些情况下不被支持

  3、不要将表单组或栅格列(column)类直接和输入框组混合使用。而是将输入框组嵌套到表单组或栅格相关元素的内部

<h3>错误示范</h3>
<div class="input-group col-xs-4">
<span class="input-group-addon" id="basic-addon1">@</span>
<input type="text" class="form-control" placeholder="Username" aria-describedby="basic-addon1">
</div>
<div class="input-group col-xs-8">
<span class="input-group-addon" id="basic-addon1">@</span>
<input type="text" class="form-control" placeholder="Username" aria-describedby="basic-addon1">
</div>
<h3>正确示范</h3>
<div class="col-xs-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">@</span>
<input type="text" class="form-control" placeholder="Username" aria-describedby="basic-addon1">
</div>
</div>
<div class="col-xs-8">
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">@</span>
<input type="text" class="form-control" placeholder="Username" aria-describedby="basic-addon1">
</div>
</div>

  4、可以添加多个(.input-group-addon 或 .input-group-btn

<div class="input-group">
<span class="input-group-addon" id="basic-addon1">@</span>
<span class="input-group-addon" id="basic-addon1">@</span>
<input type="text" class="form-control" placeholder="Username" aria-describedby="basic-addon1">
<span class="input-group-addon" id="basic-addon1">@</span>
</div>

  5、不支持在单个输入框组中添加多个表单控件

<div class="input-group">
<span class="input-group-addon" id="basic-addon1">@</span>
<input type="text" class="form-control" placeholder="Username" aria-describedby="basic-addon1">
<input type="text" class="form-control" placeholder="Username" aria-describedby="basic-addon1">
</div>

尺寸

  为 .input-group 添加相应的尺寸类,其内部包含的元素将自动调整自身的尺寸。不需要为输入框组中的每个元素重复地添加控制尺寸的类。提供了.input-group-lg和.input-group-sm,未提供超小型的样式,也许作者认为不需要

<div class="input-group input-group-lg">
<span class="input-group-addon" id="basic-addon1">@</span>
<input type="text" class="form-control" placeholder="Username" aria-describedby="basic-addon1">
</div>
<div class="input-group input-group-sm">
<span class="input-group-addon" id="basic-addon1">@</span>
<input type="text" class="form-control" placeholder="Username" aria-describedby="basic-addon1">
</div>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">@</span>
<input type="text" class="form-control" placeholder="Username" aria-describedby="basic-addon1">
</div>

额外元素

【多选框或单选框】

  可以将多选框或单选框作为额外元素添加到输入框组中

<div class="input-group">
<span class="input-group-addon">
<input type="checkbox" aria-label="...">
</span>
<input type="text" class="form-control" aria-label="...">
</div>
<div class="input-group">
<span class="input-group-addon">
<input type="radio" aria-label="...">
</span>
<input type="text" class="form-control" aria-label="...">
</div>

【按钮】

  为输入框组添加按钮需要额外添加一层嵌套,不是 .input-group-addon,而是添加 .input-group-btn 来包裹按钮元素。由于.btn按钮样式定义了各种各样的样式,其不像checkbox、radio、label等直接放到.input-group-addon样式里就可以的。所以,为了避免冲突,作者为.btn样式又单独设置了一个.input-group-btn样式,用来替换.input-group-addon作为新的addon容器

<div class="input-group">
<span class="input-group-btn">
<button class="btn btn-default" type="button">Go!</button>
</span>
<input type="text" class="form-control" placeholder="Search for...">
</div>

【按钮式下拉菜单】

  很自然,能支持按钮,也就能支持按钮式下拉菜单,不需要额外的样式支持,只需要在普通的.btn按钮上应用一个data-toggle="dropdown"属性即可

<div class="input-group">
<div class="input-group-btn">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Action <span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</div>
<input type="text" class="form-control" aria-label="...">
</div>

【分裂式按钮下拉菜单】

<div class="input-group">
<div class="input-group-btn">
<button class="btn btn-default" type="button">Action</button>
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</div>
<input type="text" class="form-control" aria-label="...">
</div>
上一篇:UVa 1025 A Spy in the Metro


下一篇:Codeforces 671D Roads in Yusland [树形DP,线段树合并]