低代码编程实战平台BUILDER · 拖拽搭建
低代码编程实战平台 Low-Code Builder
BLD-598485 表单 2026.04.12 —

轻松掌握Bootstrap:教你如何快速添加实用表单元素

Component
表单
Build
598485
Created
04.12
Updated
04.12

Bootstrap 是一个流行的前端框架,它提供了一系列的工具和组件,帮助开发者快速构建响应式和美观的网页。在 Bootstrap 中,表单元素是非常基础且重要的部分,因为它们是用户与网站交互的主要途径。下面,我将详细讲解如何使用 Bootstrap 快速添加实用的表单元素。

1. 了解Bootstrap表单布局

Bootstrap 提供了多种布局方式来创建表单,包括水平布局和垂直布局。默认情况下,Bootstrap 的表单是垂直布局,这意味着所有表单控件和标签都是堆叠的。

1.1 垂直布局

垂直布局是 Bootstrap 的默认表单布局方式,它非常适合在手机和平板设备上使用。

<form>
  <div class="form-group">
    <label for="exampleInputEmail1">邮箱地址</label>
    <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="请输入邮箱地址">
    <small id="emailHelp" class="form-text text-muted">我们不会分享您的邮箱地址给任何第三方。</small>
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">密码</label>
    <input type="password" class="form-control" id="exampleInputPassword1" placeholder="请输入密码">
  </div>
  <div class="form-check">
    <input type="checkbox" class="form-check-input" id="check1">
    <label class="form-check-label" for="check1">记住我</label>
  </div>
  <button type="submit" class="btn btn-primary">提交</button>
</form>

1.2 水平布局

如果您需要在桌面设备上创建更紧凑的表单,可以使用水平布局。

<form class="form-inline">
  <div class="form-group">
    <label for="exampleInputEmail1">邮箱地址</label>
    <input type="email" class="form-control" id="exampleInputEmail1" placeholder="请输入邮箱地址">
  </div>
  <div class="form-group mx-sm-3">
    <label for="exampleInputPassword1">密码</label>
    <input type="password" class="form-control" id="exampleInputPassword1" placeholder="请输入密码">
  </div>
  <div class="form-check mx-sm-3">
    <input type="checkbox" class="form-check-input" id="check2">
    <label class="form-check-label" for="check2">记住我</label>
  </div>
  <button type="submit" class="btn btn-primary">提交</button>
</form>

2. 表单控件

Bootstrap 提供了多种表单控件,包括输入框、选择框、文本域、复选框和单选按钮等。

2.1 输入框

输入框是表单中最常见的控件,Bootstrap 提供了多种样式和状态。

<input type="text" class="form-control" placeholder="请输入文本" aria-label="Text input with placeholder">

2.2 选择框

选择框用于创建下拉列表。

<select class="form-control">
  <option>选项 1</option>
  <option>选项 2</option>
  <option>选项 3</option>
</select>

2.3 文本域

文本域用于创建多行文本输入框。

<textarea class="form-control" rows="3"></textarea>

2.4 复选框和单选按钮

复选框和单选按钮用于创建多项选择。

<div class="form-check">
  <input type="checkbox" class="form-check-input" id="check3">
  <label class="form-check-label" for="check3">选项 1</label>
</div>
<div class="form-check">
  <input type="radio" class="form-check-input" id="radio1" name="optionsRadios" value="option1" checked>
  <label class="form-check-label" for="radio1">选项 1</label>
</div>

3. 表单验证

Bootstrap 提供了一套表单验证类,可以帮助您快速实现表单验证功能。

<form>
  <div class="form-group has-success">
    <label for="inputSuccess">成功状态</label>
    <input type="text" class="form-control" id="inputSuccess" aria-describedby="inputSuccessFeedback" placeholder="输入内容">
    <div class="form-control-feedback">验证成功!</div>
  </div>
  <div class="form-group has-warning">
    <label for="inputWarning">警告状态</label>
    <input type="text" class="form-control" id="inputWarning" aria-describedby="inputWarningFeedback" placeholder="输入内容">
    <div class="form-control-feedback">验证警告!</div>
  </div>
  <div class="form-group has-error">
    <label for="inputError">错误状态</label>
    <input type="text" class="form-control" id="inputError" aria-describedby="inputErrorFeedback" placeholder="输入内容">
    <div class="form-control-feedback">验证失败!</div>
  </div>
</form>

4. 表单工具

Bootstrap 还提供了一些表单工具,如表单控件尺寸、表单水平排列、表单状态提示等。

4.1 表单控件尺寸

<input type="text" class="form-control form-control-lg" placeholder="大尺寸">
<input type="text" class="form-control form-control-sm" placeholder="小尺寸">

4.2 表单水平排列

<form class="form-inline">
  <div class="form-group">
    <label for="inputPassword">密码</label>
    <input type="password" class="form-control" id="inputPassword" placeholder="请输入密码">
  </div>
  <div class="form-group">
    <label for="inputConfirmPassword">确认密码</label>
    <input type="password" class="form-control" id="inputConfirmPassword" placeholder="请再次输入密码">
  </div>
</form>

4.3 表单状态提示

<div class="form-group">
  <div class="form-control-static">这是静态内容</div>
</div>
<div class="form-group">
  <span class="form-text text-muted">这是一段辅助说明</span>
</div>

通过以上步骤,您已经掌握了如何使用 Bootstrap 快速添加实用的表单元素。现在,您可以开始创建美观且功能齐全的表单了。祝您开发愉快!

同件

同组件记录

Same Component