输入框组 | Input group

输入组

通过在文本输入的两侧添加文本,按钮或按钮组,轻松扩展表单控件。

基本示例

在输入的任一侧放置一个附件或按钮。您也可以在输入的两侧放置一个。我们不支持单个输入组中的多个表单控件,并且<label>必须位于输入组之外。

在 getbootstrap.com 上打开示例

<div class="input-group"> <span class="input-group-addon" id="basic-addon1">@</span> <input type="text" class="form-control" placeholder="Username" aria-label="Username" aria-describedby="basic-addon1"> </div> <br> <div class="input-group"> <input type="text" class="form-control" placeholder="Recipient's username" aria-label="Recipient's username" aria-describedby="basic-addon2"> <span class="input-group-addon" id="basic-addon2">@example.com</span> </div> <br> <label for="basic-url">Your vanity URL</label> <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> <br> <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> <br> <div class="input-group"> <span class="input-group-addon">$</span> <span class="input-group-addon">0.00</span> <input type="text" class="form-control" aria-label="Amount (to the nearest dollar)"> </div>

丈量大小

将相对表单大小类添加到.input-group自身,并且内部的内容将自动调整大小 - 无需重复每个元素上的表单控件大小类。

在 getbootstrap.com 上打开示例

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

复选框和无线电插件

将任何复选框或广播选项放在输入组的插件中,而不是文本。

在 getbootstrap.com 上打开示例

<div class="row"> <div class="col-lg-6"> <div class="input-group"> <span class="input-group-addon"> <input type="checkbox" aria-label="Checkbox for following text input"> </span> <input type="text" class="form-control" aria-label="Text input with checkbox"> </div> </div> <div class="col-lg-6"> <div class="input-group"> <span class="input-group-addon"> <input type="radio" aria-label="Radio button for following text input"> </span> <input type="text" class="form-control" aria-label="Text input with radio button"> </div> </div> </div>

多个插件

支持多个附加组件,并且可以与复选框和无线电输入版本混合使用。

在 getbootstrap.com 上打开示例

<div class="row"> <div class="col-lg-6"> <div class="input-group"> <span class="input-group-addon"> <input type="checkbox" aria-label="Checkbox for following text input"> </span> <span class="input-group-addon">$</span> <input type="text" class="form-control" aria-label="Text input with checkbox"> </div> </div> <div class="col-lg-6"> <div class="input-group"> <span class="input-group-addon">$</span> <span class="input-group-addon">0.00</span> <input type="text" class="form-control" aria-label="Text input with radio button"> </div> </div> </div>

按钮插件

输入组中的按钮必须包含在一个.input-group-btn中才能正确对齐和调整大小。由于无法覆盖的默认浏览器样式,因此这是必需的。

在 getbootstrap.com 上打开示例

<div class="row"> <div class="col-lg-6"> <div class="input-group"> <span class="input-group-btn"> <button class="btn btn-secondary" type="button">Go!</button> </span> <input type="text" class="form-control" placeholder="Search for..." aria-label="Search for..."> </div> </div> <div class="col-lg-6"> <div class="input-group"> <input type="text" class="form-control" placeholder="Search for..." aria-label="Search for..."> <span class="input-group-btn"> <button class="btn btn-secondary" type="button">Go!</button> </span> </div> </div> </div> <br> <div class="row"> <div class="col-lg-6 offset-lg-3"> <div class="input-group"> <span class="input-group-btn"> <button class="btn btn-secondary" type="button">Hate it</button> </span> <input type="text" class="form-control" placeholder="Product name" aria-label="Product name"> <span class="input-group-btn"> <button class="btn btn-secondary" type="button">Love it</button> </span> </div> </div> </div>

带下拉的按钮

在 getbootstrap.com 上打开示例

<div class="row"> <div class="col-lg-6"> <div class="input-group"> <div class="input-group-btn"> <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> Action </button> <div class="dropdown-menu"> <a class="dropdown-item" href="#">Action</a> <a class="dropdown-item" href="#">Another action</a> <a class="dropdown-item" href="#">Something else here</a> <div role="separator" class="dropdown-divider"></div> <a class="dropdown-item" href="#">Separated link</a> </div> </div> <input type="text" class="form-control" aria-label="Text input with dropdown button"> </div> </div> <div class="col-lg-6"> <div class="input-group"> <input type="text" class="form-control" aria-label="Text input with dropdown button"> <div class="input-group-btn"> <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> Action </button> <div class="dropdown-menu dropdown-menu-right"> <a class="dropdown-item" href="#">Action</a> <a class="dropdown-item" href="#">Another action</a> <a class="dropdown-item" href="#">Something else here</a> <div role="separator" class="dropdown-divider"></div> <a class="dropdown-item" href="#">Separated link</a> </div> </div> </div> </div> </div>

分段的按钮

在 getbootstrap.com 上打开示例

<div class="row"> <div class="col-lg-6"> <div class="input-group"> <div class="input-group-btn"> <button type="button" class="btn btn-secondary">Action</button> <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> <span class="sr-only">Toggle Dropdown</span> </button> <div class="dropdown-menu"> <a class="dropdown-item" href="#">Action</a> <a class="dropdown-item" href="#">Another action</a> <a class="dropdown-item" href="#">Something else here</a> <div role="separator" class="dropdown-divider"></div> <a class="dropdown-item" href="#">Separated link</a> </div> </div> <input type="text" class="form-control" aria-label="Text input with segmented button dropdown"> </div> </div> <div class="col-lg-6"> <div class="input-group"> <input type="text" class="form-control" aria-label="Text input with segmented button dropdown"> <div class="input-group-btn"> <button type="button" class="btn btn-secondary">Action</button> <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> <span class="sr-only">Toggle Dropdown</span> </button> <div class="dropdown-menu dropdown-menu-right"> <a class="dropdown-item" href="#">Action</a> <a class="dropdown-item" href="#">Another action</a> <a class="dropdown-item" href="#">Something else here</a> <div role="separator" class="dropdown-divider"></div> <a class="dropdown-item" href="#">Separated link</a> </div> </div> </div> </div> </div>

无障碍

如果您没有为每个输入添加标签,屏幕阅读器将会对您的表单造成麻烦。对于这些输入组,确保将任何附加标签或功能传送给辅助技术。

要使用的确切技术(使用.sr-only类隐藏<label>元素,或使用aria-label和aria-labelledby属性,可能结合使用aria-describedby)以及需要传达的附加信息取决于您实现的接口窗口小部件的确切类型。本节中的示例提供了一些建议的特定于案例的方法。