本章将講解 Bootstrap 支援的另一個特性,輸入框組。輸入框組擴充自 表單控件。使用輸入框組,您可以很容易地向基于文本的輸入框添加作為字首和字尾的文本或按鈕。
通過向輸入域添加字首和字尾的内容,您可以向使用者輸入添加公共的元素。例如,您可以添加美元符号,或者在 Twitter 使用者名前添加 @,或者應用程式接口所需要的其他公共的元素。
向 <b>.form-control</b> 添加字首或字尾元素的步驟如下:
把字首或字尾元素放在一個帶有 class <b>.input-group</b> 的 <div> 中。
接着,在相同的 <div> 内,在 class 為 <b>.input-group-addon</b> 的 <span> 内放置額外的内容。
把該 <span> 放置在 <input> 元素的前面或者後面。
為了保持跨浏覽器的相容性,請避免使用 <select> 元素,因為它們在 WebKit 浏覽器中不能完全渲染出效果。也不要直接向表單組應用輸入框組的 class,輸入框組是一個孤立的元件。![]()
Bootstrap 輸入框組
下面的執行個體示範了基本的輸入框組:
<div style="padding: 100px 100px 10px;">
<form class="bs-example bs-example-form" role="form">
<div class="input-group">
<span class="input-group-addon">@</span>
<input type="text" class="form-control" placeholder="twitterhandle">
</div>
<br>
<input type="text" class="form-control">
<span class="input-group-addon">.00</span>
<span class="input-group-addon">$</span>
</form>
結果如下所示:
您可以通過向 <b>.input-group</b> 添加相對表單大小的 class(比如 <b>.input-group-lg、input-group-sm</b>)來改變輸入框組的大小。輸入框中的内容會自動調整大小。
下面的執行個體示範了這點:
<div class="input-group input-group-lg">
<input type="text" class="form-control" placeholder="Twitterhandle">
<div class="input-group input-group-sm">
您可以把複選框和單選插件作為輸入框組的字首或者字尾元素,如下面的執行個體所示:
<div class="row">
<div class="col-lg-6">
<span class="input-group-addon">
<input type="checkbox"></span>
</div><!-- /input-group -->
</div><!-- /.col-lg-6 -->
<input type="radio"></span>
</div><!-- /.row -->
您也可以把按鈕作為輸入框組的字首或者字尾元素,這個時候您就不是添加 <b>.input-group-addon</b> class,您需要使用 class <b>.input-group-btn</b> 來包裹按鈕。這是必需的,因為預設的浏覽器樣式不會被重寫。下面的執行個體示範了這點:
<span class="input-group-btn">
<button class="btn btn-default" type="button">Go!</button>
</span>
在輸入框組中添加帶有下拉菜單的按鈕,隻需要簡單地在一個 <b>.input-group-btn</b> class 中包裹按鈕和下拉菜單即可,如下面的執行個體所示:
<div class="input-group-btn">
<button type="button" class="btn btn-default
dropdown-toggle" data-toggle="dropdown">下拉菜單
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li>
<a href="#">功能</a>
</li>
<a href="#">另一個功能</a>
<a href="#">其他</a>
<li class="divider"></li>
<a href="#">分離的連結</a>
</ul>
</div><!-- /btn-group -->
<ul class="dropdown-menu pull-right">
在輸入框組中添加帶有下拉菜單的分割按鈕,使用與下拉菜單按鈕大緻相同的樣式,但是對下拉菜單添加了主要的功能,如下面的執行個體所示:
<button type="button" class="btn btn-default" tabindex="-1">下拉菜單</button>
dropdown-toggle" data-toggle="dropdown" tabindex="-1">
<span class="sr-only">切換下拉菜單</span>