首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

表单 | Forms

表单控件样式、布局选项和自定义组件的示例和使用指南,用于创建多种窗体。

概述

引导程序的窗体控件扩展到我们重新启动的表单样式上课。使用这些类选择它们的自定义显示,以便在浏览器和设备之间进行更一致的呈现。

确保使用适当的type属性对所有输入%28e。g.email有关电子邮件地址或number关于数字信息%29,以利用较新的输入控件,如电子邮件验证,号码选择,等等。

下面是一个演示Bootstrap表单样式的快速示例。继续阅读有关所需类、表单布局等方面的文档。

代码语言:javascript
复制
<form>
  <div class="form-group">
    <label for="exampleInputEmail1">Email address</label>
    <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
    <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Password</label>
    <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
  </div>
  <div class="form-check">
    <label class="form-check-label">
      <input type="checkbox" class="form-check-input">
      Check me out
    </label>
  </div>
  <button type="submit" class="btn btn-primary">Submit</button>
</form>

表单控件

文本窗体控件<input>S,<select>S,和<textarea>s-是用.form-control上课。包括一般外观、焦点状态、大小大小等样式。

一定要探索我们的定制表格以进一步的风格<select>S.

代码语言:javascript
复制
<form>
  <div class="form-group">
    <label for="exampleFormControlInput1">Email address</label>
    <input type="email" class="form-control" id="exampleFormControlInput1" placeholder="name@example.com">
  </div>
  <div class="form-group">
    <label for="exampleFormControlSelect1">Example select</label>
    <select class="form-control" id="exampleFormControlSelect1">
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
      <option>5</option>
    </select>
  </div>
  <div class="form-group">
    <label for="exampleFormControlSelect2">Example multiple select</label>
    <select multiple class="form-control" id="exampleFormControlSelect2">
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
      <option>5</option>
    </select>
  </div>
  <div class="form-group">
    <label for="exampleFormControlTextarea1">Example textarea</label>
    <textarea class="form-control" id="exampleFormControlTextarea1" rows="3"></textarea>
  </div>
</form>

对于文件输入,交换.form-control.form-control-file...

代码语言:javascript
复制
<form>
  <div class="form-group">
    <label for="exampleFormControlFile1">Example file input</label>
    <input type="file" class="form-control-file" id="exampleFormControlFile1">
  </div>
</form>

上浆

使用类似的类设置高度.form-control-lg.form-control-sm...

代码语言:javascript
复制
<input class="form-control form-control-lg" type="text" placeholder=".form-control-lg">
<input class="form-control" type="text" placeholder="Default input">
<input class="form-control form-control-sm" type="text" placeholder=".form-control-sm">
代码语言:javascript
复制
<select class="form-control form-control-lg">
  <option>Large select</option>
</select>
<select class="form-control">
  <option>Default select</option>
</select>
<select class="form-control form-control-sm">
  <option>Small select</option>
</select>

只读

添加readonly属性,以防止修改输入的值。只读文本框看起来更轻(就像被禁用的文本框一样),但保留标准光标。

代码语言:javascript
复制
<input class="form-control" type="text" placeholder="Readonly input here…" readonly>

只读纯文本

如果你想<input readonly>元素样式为纯文本的窗体中的元素,请使用.form-control-plaintext类以移除默认窗体字段的样式,并保留正确的页边距和填充。

代码语言:javascript
复制
<form>
  <div class="form-group row">
    <label for="staticEmail" class="col-sm-2 col-form-label">Email</label>
    <div class="col-sm-10">
      <input type="text" readonly class="form-control-plaintext" id="staticEmail" value="email@example.com">
    </div>
  </div>
  <div class="form-group row">
    <label for="inputPassword" class="col-sm-2 col-form-label">Password</label>
    <div class="col-sm-10">
      <input type="password" class="form-control" id="inputPassword" placeholder="Password">
    </div>
  </div>
</form>
代码语言:javascript
复制
<form class="form-inline">
  <div class="form-group">
    <label for="staticEmail2" class="sr-only">Email</label>
    <input type="text" readonly class="form-control-plaintext" id="staticEmail2" value="email@example.com">
  </div>
  <div class="form-group mx-sm-3">
    <label for="inputPassword2" class="sr-only">Password</label>
    <input type="password" class="form-control" id="inputPassword2" placeholder="Password">
  </div>
  <button type="submit" class="btn btn-primary">Confirm identity</button>
</form>

复选框和收音机

默认复选框和收音机是在以下帮助下进行改进的:.form-check,,,为这两种输入类型提供一个单独的类,以改进它们的HTML元素的布局和行为复选框用于选择列表中的一个或多个选项,而收音机则用于从多个选项中选择一个选项。

支持禁用复选框和收音机,但要提供not-allowed光标在父节点的悬停上。<label>,则需要添加.disabled类传递给父类。.form-check禁用类还将减轻文本颜色,以帮助指示输入的状态。

默认%28堆栈%29

默认情况下,任何数量的复选框和收音机都将垂直堆叠,并适当地用.form-check...

代码语言:javascript
复制
<div class="form-check">
  <label class="form-check-label">
    <input class="form-check-input" type="checkbox" value="">
    Option one is this and that&mdash;be sure to include why it's great
  </label>
</div>
<div class="form-check disabled">
  <label class="form-check-label">
    <input class="form-check-input" type="checkbox" value="" disabled>
    Option two is disabled
  </label>
</div>
代码语言:javascript
复制
<div class="form-check">
  <label class="form-check-label">
    <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios1" value="option1" checked>
    Option one is this and that&mdash;be sure to include why it's great
  </label>
</div>
<div class="form-check">
  <label class="form-check-label">
    <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios2" value="option2">
    Option two can be something else and selecting it will deselect option one
  </label>
</div>
<div class="form-check disabled">
  <label class="form-check-label">
    <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios3" value="option3" disabled>
    Option three is disabled
  </label>
</div>

内联

将同一水平行上的复选框或收音机分组,方法是添加.form-check-inline给任何人.form-check...

代码语言:javascript
复制
<div class="form-check form-check-inline">
  <label class="form-check-label">
    <input class="form-check-input" type="checkbox" id="inlineCheckbox1" value="option1"> 1
  </label>
</div>
<div class="form-check form-check-inline">
  <label class="form-check-label">
    <input class="form-check-input" type="checkbox" id="inlineCheckbox2" value="option2"> 2
  </label>
</div>
<div class="form-check form-check-inline disabled">
  <label class="form-check-label">
    <input class="form-check-input" type="checkbox" id="inlineCheckbox3" value="option3" disabled> 3
  </label>
</div>
代码语言:javascript
复制
<div class="form-check form-check-inline">
  <label class="form-check-label">
    <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio1" value="option1"> 1
  </label>
</div>
<div class="form-check form-check-inline">
  <label class="form-check-label">
    <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio2" value="option2"> 2
  </label>
</div>
<div class="form-check form-check-inline disabled">
  <label class="form-check-label">
    <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio3" value="option3" disabled> 3
  </label>
</div>

无标签

.position-static对内部的输入.form-check没有任何标签文字。记住仍然要为辅助技术提供某种形式的标签,例如,使用aria-label29%。

代码语言:javascript
复制
<div class="form-check">
  <label class="form-check-label">
    <input class="form-check-input position-static" type="checkbox" id="blankCheckbox" value="option1" aria-label="...">
  </label>
</div>
<div class="form-check">
  <label class="form-check-label">
    <input class="form-check-input position-static" type="radio" name="blankRadio" id="blankRadio1" value="option1" aria-label="...">
  </label>
</div>

布局

因为Bootstrap适用display: blockwidth: 100%对于几乎所有的窗体控件,窗体默认将垂直堆栈。其他类可用于根据每个表单更改此布局。

形式群

.form-group类是向窗体添加某些结构的最简单方法。它的唯一目的是提供margin-bottom围绕标签和控制配对。作为奖励,因为它是一个类,您可以使用它<fieldset>S,<div>S或几乎任何其他元素。

代码语言:javascript
复制
<form>
  <div class="form-group">
    <label class="col-form-label" for="formGroupExampleInput">Example label</label>
    <input type="text" class="form-control" id="formGroupExampleInput" placeholder="Example input">
  </div>
  <div class="form-group">
    <label class="col-form-label" for="formGroupExampleInput2">Another label</label>
    <input type="text" class="form-control" id="formGroupExampleInput2" placeholder="Another input">
  </div>
</form>

表格网格

可以使用我们的网格类构建更复杂的表单。将这些用于需要多列、不同宽度和其他对齐选项的表单布局。

代码语言:javascript
复制
<form>
  <div class="row">
    <div class="col">
      <input type="text" class="form-control" placeholder="First name">
    </div>
    <div class="col">
      <input type="text" class="form-control" placeholder="Last name">
    </div>
  </div>
</form>

表格行

你也可以交换.row.form-row,这是我们标准网格行的一个变体,它覆盖了默认的列排水沟,以使布局更加紧凑。

代码语言:javascript
复制
<form>
  <div class="form-row">
    <div class="col">
      <input type="text" class="form-control" placeholder="First name">
    </div>
    <div class="col">
      <input type="text" class="form-control" placeholder="Last name">
    </div>
  </div>
</form>

还可以用网格系统创建更复杂的布局。

代码语言:javascript
复制
<form>
  <div class="form-row">
    <div class="form-group col-md-6">
      <label for="inputEmail4">Email</label>
      <input type="email" class="form-control" id="inputEmail4" placeholder="Email">
    </div>
    <div class="form-group col-md-6">
      <label for="inputPassword4">Password</label>
      <input type="password" class="form-control" id="inputPassword4" placeholder="Password">
    </div>
  </div>
  <div class="form-group">
    <label for="inputAddress">Address</label>
    <input type="text" class="form-control" id="inputAddress" placeholder="1234 Main St">
  </div>
  <div class="form-group">
    <label for="inputAddress2">Address 2</label>
    <input type="text" class="form-control" id="inputAddress2" placeholder="Apartment, studio, or floor">
  </div>
  <div class="form-row">
    <div class="form-group col-md-6">
      <label for="inputCity">City</label>
      <input type="text" class="form-control" id="inputCity">
    </div>
    <div class="form-group col-md-4">
      <label for="inputState">State</label>
      <select id="inputState" class="form-control">
        <option selected>Choose...</option>
        <option>...</option>
      </select>
    </div>
    <div class="form-group col-md-2">
      <label for="inputZip">Zip</label>
      <input type="text" class="form-control" id="inputZip">
    </div>
  </div>
  <div class="form-group">
    <div class="form-check">
      <label class="form-check-label">
        <input class="form-check-input" type="checkbox"> Check me out
      </label>
    </div>
  </div>
  <button type="submit" class="btn btn-primary">Sign in</button>
</form>

水平形式

通过添加.row类来形成组,并使用.col-*-*类指定标签和控件的宽度。

一定要加上.col-form-label敬你的<label>S也是如此,因此它们垂直地以其关联的窗体控件为中心。为<legend>元素,您可以使用.col-form-legend使它们看起来像普通的<label>元素。

代码语言:javascript
复制
<form>
  <div class="form-group row">
    <label for="inputEmail3" class="col-sm-2 col-form-label">Email</label>
    <div class="col-sm-10">
      <input type="email" class="form-control" id="inputEmail3" placeholder="Email">
    </div>
  </div>
  <div class="form-group row">
    <label for="inputPassword3" class="col-sm-2 col-form-label">Password</label>
    <div class="col-sm-10">
      <input type="password" class="form-control" id="inputPassword3" placeholder="Password">
    </div>
  </div>
  <fieldset class="form-group">
    <div class="row">
      <legend class="col-form-legend col-sm-2">Radios</legend>
      <div class="col-sm-10">
        <div class="form-check">
          <label class="form-check-label">
            <input class="form-check-input" type="radio" name="gridRadios" id="gridRadios1" value="option1" checked>
            Option one is this and that&mdash;be sure to include why it's great
          </label>
        </div>
        <div class="form-check">
          <label class="form-check-label">
            <input class="form-check-input" type="radio" name="gridRadios" id="gridRadios2" value="option2">
            Option two can be something else and selecting it will deselect option one
          </label>
        </div>
        <div class="form-check disabled">
          <label class="form-check-label">
            <input class="form-check-input" type="radio" name="gridRadios" id="gridRadios3" value="option3" disabled>
            Option three is disabled
          </label>
        </div>
      </div>
    </div>
  </fieldset>
  <div class="form-group row">
    <div class="col-sm-2">Checkbox</div>
    <div class="col-sm-10">
      <div class="form-check">
        <label class="form-check-label">
          <input class="form-check-input" type="checkbox"> Check me out
        </label>
      </div>
    </div>
  </div>
  <div class="form-group row">
    <div class="col-sm-10">
      <button type="submit" class="btn btn-primary">Sign in</button>
    </div>
  </div>
</form>
水平标号尺寸

确保使用.col-form-label-sm.col-form-label-lg敬你的<label>s正确地跟随.form-control-lg.form-control-sm...

代码语言:javascript
复制
<form>
  <div class="form-group row">
    <label for="colFormLabelSm" class="col-sm-2 col-form-label col-form-label-sm">Email</label>
    <div class="col-sm-10">
      <input type="email" class="form-control form-control-sm" id="colFormLabelSm" placeholder="col-form-label-sm">
    </div>
  </div>
  <div class="form-group row">
    <label for="colFormLabel" class="col-sm-2 col-form-label">Email</label>
    <div class="col-sm-10">
      <input type="email" class="form-control" id="colFormLabel" placeholder="col-form-label">
    </div>
  </div>
  <div class="form-group row">
    <label for="colFormLabelLg" class="col-sm-2 col-form-label col-form-label-lg">Email</label>
    <div class="col-sm-10">
      <input type="email" class="form-control form-control-lg" id="colFormLabelLg" placeholder="col-form-label-lg">
    </div>
  </div>
</form>

列尺寸

如前面的示例所示,我们的网格系统允许您放置任意数量的.col.row.form-row.他们会平分可用的宽度。您还可以选择列的一个子集来占用更多或更少的空间,而其余的部分则会占用.cols与其他列类平分,具体列类如下.col-7...

代码语言:javascript
复制
<form>
  <div class="form-row">
    <div class="col-7">
      <input type="text" class="form-control" placeholder="City">
    </div>
    <div class="col">
      <input type="text" class="form-control" placeholder="State">
    </div>
    <div class="col">
      <input type="text" class="form-control" placeholder="Zip">
    </div>
  </div>
</form>

自动尺寸

下面的示例使用一个Flexbox实用程序垂直地将内容和更改中心化。.col.col-auto这样,您的列只需要占用足够的空间。换句话说,列本身根据内容大小。

代码语言:javascript
复制
<form>
  <div class="form-row align-items-center">
    <div class="col-auto">
      <label class="sr-only" for="inlineFormInput">Name</label>
      <input type="text" class="form-control mb-2 mb-sm-0" id="inlineFormInput" placeholder="Jane Doe">
    </div>
    <div class="col-auto">
      <label class="sr-only" for="inlineFormInputGroup">Username</label>
      <div class="input-group mb-2 mb-sm-0">
        <div class="input-group-addon">@</div>
        <input type="text" class="form-control" id="inlineFormInputGroup" placeholder="Username">
      </div>
    </div>
    <div class="col-auto">
      <div class="form-check mb-2 mb-sm-0">
        <label class="form-check-label">
          <input class="form-check-input" type="checkbox"> Remember me
        </label>
      </div>
    </div>
    <div class="col-auto">
      <button type="submit" class="btn btn-primary">Submit</button>
    </div>
  </div>
</form>

然后,您可以再次将其与特定大小的列类混合。

代码语言:javascript
复制
<form>
  <div class="form-row align-items-center">
    <div class="col-sm-3">
      <label class="sr-only" for="inlineFormInputName">Name</label>
      <input type="text" class="form-control mb-2 mb-sm-0" id="inlineFormInputName" placeholder="Jane Doe">
    </div>
    <div class="col-sm-3">
      <label class="sr-only" for="inlineFormInputGroupUsername">Username</label>
      <div class="input-group mb-2 mb-sm-0">
        <div class="input-group-addon">@</div>
        <input type="text" class="form-control" id="inlineFormInputGroupUsername" placeholder="Username">
      </div>
    </div>
    <div class="col-auto">
      <div class="form-check mb-2 mb-sm-0">
        <label class="form-check-label">
          <input class="form-check-input" type="checkbox"> Remember me
        </label>
      </div>
    </div>
    <div class="col-auto">
      <button type="submit" class="btn btn-primary">Submit</button>
    </div>
  </div>
</form>

当然还有自定义窗体控件是支持的。

代码语言:javascript
复制
<form>
  <div class="form-row align-items-center">
    <div class="col-auto">
      <label class="mr-sm-2" for="inlineFormCustomSelect">Preference</label>
      <select class="custom-select mb-2 mr-sm-2 mb-sm-0" id="inlineFormCustomSelect">
        <option selected>Choose...</option>
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
      </select>
    </div>
    <div class="col-auto">
      <label class="custom-control custom-checkbox mb-2 mr-sm-2 mb-sm-0">
        <input type="checkbox" class="custom-control-input">
        <span class="custom-control-indicator"></span>
        <span class="custom-control-description">Remember my preference</span>
      </label>
    </div>
    <div class="col-auto">
      <button type="submit" class="btn btn-primary">Submit</button>
    </div>
  </div>
</form>

内联形式

使用.form-inline类以在单个水平行上显示一系列标签、窗体控件和按钮。内联窗体中的窗体控件与其默认状态略有不同。

  • 控件是display: flex,折叠任何HTML空白,并允许您提供对齐控制间距和挠曲箱公用设施。
  • 控件和输入组接收width: auto若要重写引导默认设置,请执行以下操作width: 100%...
  • 控制仅在宽度至少为576 px的视图中显示内联。说明移动设备上的窄视口。

您可能需要手动处理各个窗体控件的宽度和对齐方式。间距效用%28,如%29所示。最后,确保始终包括<label>使用每个窗体控件,即使您需要对非屏幕阅读器访问者隐藏它.sr-only...

代码语言:javascript
复制
<form class="form-inline">
  <label class="sr-only" for="inlineFormInputName2">Name</label>
  <input type="text" class="form-control mb-2 mr-sm-2 mb-sm-0" id="inlineFormInputName2" placeholder="Jane Doe">

  <label class="sr-only" for="inlineFormInputGroupUsername2">Username</label>
  <div class="input-group mb-2 mr-sm-2 mb-sm-0">
    <div class="input-group-addon">@</div>
    <input type="text" class="form-control" id="inlineFormInputGroupUsername2" placeholder="Username">
  </div>

  <div class="form-check mb-2 mr-sm-2 mb-sm-0">
    <label class="form-check-label">
      <input class="form-check-input" type="checkbox"> Remember me
    </label>
  </div>

  <button type="submit" class="btn btn-primary">Submit</button>
</form>

还支持自定义窗体控件和选择。

代码语言:javascript
复制
<form class="form-inline">
  <label class="mr-sm-2" for="inlineFormCustomSelectPref">Preference</label>
  <select class="custom-select mb-2 mr-sm-2 mb-sm-0" id="inlineFormCustomSelectPref">
    <option selected>Choose...</option>
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
  </select>

  <label class="custom-control custom-checkbox mb-2 mr-sm-2 mb-sm-0">
    <input type="checkbox" class="custom-control-input">
    <span class="custom-control-indicator"></span>
    <span class="custom-control-description">Remember my preference</span>
  </label>

  <button type="submit" class="btn btn-primary">Submit</button>
</form>

隐藏标签的替代方案

如果没有为每个输入都包含一个标签,屏幕阅读器等辅助技术就会给表单带来麻烦。对于这些内联窗体,可以使用.sr-only上课。还有其他为辅助技术提供标签的方法,例如aria-label,,,aria-labelledbytitle属性。如果这些技术都没有出现,辅助技术可能会使用placeholder属性(如果存在),但请注意placeholder作为其他标签方法的替代品,不建议使用其他标签方法。

帮助文本

可以使用以下方法创建窗体中的块级帮助文本:.form-text%28以前称为.help-block在v3%29。内联帮助文本可以使用任何内联HTML元素和实用程序类灵活实现,如.text-muted...

将帮助文本与窗体控件关联

帮助文本应该显式地与它所关联的窗体控件关联,使用aria-describedby属性。这将确保辅助技术--如屏幕阅读器--在用户聚焦或进入控件时宣布此帮助文本。

输入下面的帮助文本可以用.form-text本课程包括display: block并在上面的输入中添加一些顶部空间,以便于间距。

代码语言:javascript
复制
<label for="inputPassword5">Password</label>
<input type="password" id="inputPassword5" class="form-control" aria-describedby="passwordHelpBlock">
<small id="passwordHelpBlock" class="form-text text-muted">
  Your password must be 8-20 characters long, contain letters and numbers, and must not contain spaces, special characters, or emoji.
</small>

内联文本可以使用任何典型的内联HTML元素%28<small>,,,<span>,或者其他%29的东西,只有一个实用程序类。

代码语言:javascript
复制
<form class="form-inline">
  <div class="form-group">
    <label for="inputPassword6">Password</label>
    <input type="password" id="inputPassword6" class="form-control mx-sm-3" aria-describedby="passwordHelpInline">
    <small id="passwordHelpInline" class="text-muted">
      Must be 8-20 characters long.
    </small>
  </div>
</form>

残疾表格

添加disabled在输入上设置布尔属性,以防止用户交互,并使其看起来更轻。

代码语言:javascript
复制
<input class="form-control" id="disabledInput" type="text" placeholder="Disabled input here..." disabled>

添加disabled属性为<fieldset>若要禁用所有控件,请执行以下操作。

代码语言:javascript
复制
<form>
  <fieldset disabled>
    <div class="form-group">
      <label for="disabledTextInput">Disabled input</label>
      <input type="text" id="disabledTextInput" class="form-control" placeholder="Disabled input">
    </div>
    <div class="form-group">
      <label for="disabledSelect">Disabled select menu</label>
      <select id="disabledSelect" class="form-control">
        <option>Disabled select</option>
      </select>
    </div>
    <div class="form-check">
      <label class="form-check-label">
        <input class="form-check-input" type="checkbox"> Can't check this
      </label>
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
  </fieldset>
</form>

有锚的警告

默认情况下,浏览器将处理所有本机窗体控件%28<input>,,,<select><button>元素%29在<fieldset disabled>由于禁用,防止键盘和鼠标在它们上的交互。但是,如果您的表单还包括<a ... class="btn btn-*">元素,这些元素只会被赋予pointer-events: none.如有关按钮禁用状态%28,特别是在锚元素%29的子部分中,此css属性尚未标准化,并且在Opera 18及以下或InternetExplorer 10中不完全支持,也不会阻止键盘用户聚焦或激活这些链接。因此,为了安全起见,请使用自定义JavaScript禁用此类链接。

跨浏览器兼容性

虽然Bootstrap将在所有浏览器中应用这些样式,但InternetExplorer 11及以下版本并不完全支持disabled属性的<fieldset>使用自定义JavaScript禁用这些浏览器中的字段集。

验证

通过HTML 5表单验证向用户提供有价值的、可操作的反馈-在我们支持的所有浏览器中都可以使用。从浏览器默认验证反馈中选择,或者使用内置类和初学者JavaScript实现自定义消息。

我们极力推荐本机浏览器默认的自定义验证样式不会通知屏幕阅读器。

它是如何工作的

下面是表单验证如何与Bootstrap一起工作:

  • HTML表单验证通过css的两个伪类应用,:invalid:valid它适用于<input>,,,<select>,和<textarea>元素。
  • 引导范围:invalid:valid风格到父母.was-validated类,通常应用于<form>否则,任何没有值的必需字段在页面加载时都会显示为无效。通过这种方式,您可以选择何时激活它们(通常是在表单提交尝试%29之后)。
  • 作为退路,.is-invalid.is-valid类可以代替伪类。服务器端验证.他们不需要.was-validated家长班。
  • 由于css工作方式上的限制,目前%29不能将样式应用到<label>在DOM中的表单控件之前,无需自定义JavaScript的帮助。
  • 所有现代浏览器都支持约束验证API,用于验证表单控件的一系列JavaScript方法。
  • 反馈消息可以利用浏览器默认值%28不同的每个浏览器,和无法通过CSS%29或我们的自定义反馈样式与额外的HTML和CSS。
  • 您可以提供自定义有效性消息。setCustomValidity用JavaScript写的。

考虑到这一点,请考虑以下演示,用于我们的自定义表单验证样式、可选服务器端类和浏览器默认值。

定制风格

对于自定义引导窗体验证消息,您需要添加novalidate的布尔属性<form>这将禁用浏览器默认反馈工具提示,但仍然提供对JavaScript表单验证API的访问。尝试提交下面的表单;我们的JavaScript将拦截Submit按钮并将反馈转发给您。

尝试提交时,您将看到:invalid:valid应用于窗体控件的样式。

代码语言:javascript
复制
<form class="container" id="needs-validation" novalidate>
  <div class="row">
    <div class="col-md-6 mb-3">
      <label for="validationCustom01">First name</label>
      <input type="text" class="form-control" id="validationCustom01" placeholder="First name" value="Mark" required>
    </div>
    <div class="col-md-6 mb-3">
      <label for="validationCustom02">Last name</label>
      <input type="text" class="form-control" id="validationCustom02" placeholder="Last name" value="Otto" required>
    </div>
  </div>
  <div class="row">
    <div class="col-md-6 mb-3">
      <label for="validationCustom03">City</label>
      <input type="text" class="form-control" id="validationCustom03" placeholder="City" required>
      <div class="invalid-feedback">
        Please provide a valid city.
      </div>
    </div>
    <div class="col-md-3 mb-3">
      <label for="validationCustom04">State</label>
      <input type="text" class="form-control" id="validationCustom04" placeholder="State" required>
      <div class="invalid-feedback">
        Please provide a valid state.
      </div>
    </div>
    <div class="col-md-3 mb-3">
      <label for="validationCustom05">Zip</label>
      <input type="text" class="form-control" id="validationCustom05" placeholder="Zip" required>
      <div class="invalid-feedback">
        Please provide a valid zip.
      </div>
    </div>
  </div>
  <button class="btn btn-primary" type="submit">Submit form</button>
</form>

<script>
// Example starter JavaScript for disabling form submissions if there are invalid fields
(function() {
  'use strict';

  window.addEventListener('load', function() {
    var form = document.getElementById('needs-validation');
    form.addEventListener('submit', function(event) {
      if (form.checkValidity() === false) {
        event.preventDefault();
        event.stopPropagation();
      }
      form.classList.add('was-validated');
    }, false);
  }, false);
})();
</script>

浏览器默认值

对自定义验证、反馈消息或编写JavaScript来更改表单行为不感兴趣?很好,您可以使用浏览器默认设置。试着提交下面的表格。根据浏览器和操作系统的不同,您将看到一种略有不同的反馈样式。

虽然这些反馈样式不能使用CSS样式,但仍然可以通过JavaScript定制反馈文本。

代码语言:javascript
复制
<form>
  <div class="row">
    <div class="col-md-6 mb-3">
      <label for="validationDefault01">First name</label>
      <input type="text" class="form-control" id="validationDefault01" placeholder="First name" value="Mark" required>
    </div>
    <div class="col-md-6 mb-3">
      <label for="validationDefault02">Last name</label>
      <input type="text" class="form-control" id="validationDefault02" placeholder="Last name" value="Otto" required>
    </div>
  </div>
  <div class="row">
    <div class="col-md-6 mb-3">
      <label for="validationDefault03">City</label>
      <input type="text" class="form-control" id="validationDefault03" placeholder="City" required>
      <div class="invalid-feedback">
        Please provide a valid city.
      </div>
    </div>
    <div class="col-md-3 mb-3">
      <label for="validationDefault04">State</label>
      <input type="text" class="form-control" id="validationDefault04" placeholder="State" required>
      <div class="invalid-feedback">
        Please provide a valid state.
      </div>
    </div>
    <div class="col-md-3 mb-3">
      <label for="validationDefault05">Zip</label>
      <input type="text" class="form-control" id="validationDefault05" placeholder="Zip" required>
      <div class="invalid-feedback">
        Please provide a valid zip.
      </div>
    </div>
  </div>

  <button class="btn btn-primary" type="submit">Submit form</button>
</form>

服务器端

我们建议使用客户端验证,但如果需要服务器端,则可以使用.is-invalid.is-valid注意.invalid-feedback也支持这些类。

代码语言:javascript
复制
<form>
  <div class="row">
    <div class="col-md-6 mb-3">
      <label for="validationServer01">First name</label>
      <input type="text" class="form-control is-valid" id="validationServer01" placeholder="First name" value="Mark" required>
    </div>
    <div class="col-md-6 mb-3">
      <label for="validationServer02">Last name</label>
      <input type="text" class="form-control is-valid" id="validationServer02" placeholder="Last name" value="Otto" required>
    </div>
  </div>
  <div class="row">
    <div class="col-md-6 mb-3">
      <label for="validationServer03">City</label>
      <input type="text" class="form-control is-invalid" id="validationServer03" placeholder="City" required>
      <div class="invalid-feedback">
        Please provide a valid city.
      </div>
    </div>
    <div class="col-md-3 mb-3">
      <label for="validationServer04">State</label>
      <input type="text" class="form-control is-invalid" id="validationServer04" placeholder="State" required>
      <div class="invalid-feedback">
        Please provide a valid state.
      </div>
    </div>
    <div class="col-md-3 mb-3">
      <label for="validationServer05">Zip</label>
      <input type="text" class="form-control is-invalid" id="validationServer05" placeholder="Zip" required>
      <div class="invalid-feedback">
        Please provide a valid zip.
      </div>
    </div>
  </div>

  <button class="btn btn-primary" type="submit">Submit form</button>
</form>

支持的标签元素

我们的示例表单显示了本机文本。<input>S以上,但表单验证样式也适用于我们的自定义窗体控件。

代码语言:javascript
复制
<form class="was-validated">
  <label class="custom-control custom-checkbox">
    <input type="checkbox" class="custom-control-input" required>
    <span class="custom-control-indicator"></span>
    <span class="custom-control-description">Check this custom checkbox</span>
  </label>

  <div class="custom-controls-stacked d-block my-3">
    <label class="custom-control custom-radio">
      <input id="radioStacked1" name="radio-stacked" type="radio" class="custom-control-input" required>
      <span class="custom-control-indicator"></span>
      <span class="custom-control-description">Toggle this custom radio</span>
    </label>
    <label class="custom-control custom-radio">
      <input id="radioStacked2" name="radio-stacked" type="radio" class="custom-control-input" required>
      <span class="custom-control-indicator"></span>
      <span class="custom-control-description">Or toggle this other custom radio</span>
    </label>
  </div>

  <select class="custom-select d-block my-3" required>
    <option value="">Open this select menu</option>
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
  </select>

  <label class="custom-file">
    <input type="file" id="file" class="custom-file-input" required>
    <span class="custom-file-control"></span>
  </label>
</form>

定制表格

为了实现更多的自定义和跨浏览器一致性,请使用我们完全自定义的表单元素来替换浏览器默认设置。它们构建在语义和可访问标记之上,因此它们是任何默认表单控件的可靠替代品。

复选框和单选框

每个复选框和单选框都封装在<label> 中,原因有三:

  • 它为检查控件提供了更大的命中区域。
  • 它提供了一个有用的语义包装器来帮助我们替换默认的<input>S.
  • 它触发<input>自动,意味着不需要JavaScript。

我们隐藏缺省值<input>带着opacity并使用.custom-control-indicator若要在其位置生成新的自定义窗体指示器,请执行以下操作。不幸的是,我们不能仅仅从<input>因为CSS的content不适用于那个元素。

我们使用兄弟姐妹选择器%28~%29<input>类州:checked-正确的样式,我们的定制表格指示器。当与.custom-control-description类,我们还可以根据<input>状态。

在检查状态中,我们使用Base 64嵌入式SVG图标开放标志这为我们在浏览器和设备之间设置样式和定位提供了最好的控制。

复选框

代码语言:javascript
复制
<label class="custom-control custom-checkbox">
  <input type="checkbox" class="custom-control-input">
  <span class="custom-control-indicator"></span>
  <span class="custom-control-description">Check this custom checkbox</span>
</label>

自定义复选框也可以利用:indeterminate类,当通过JavaScript%28手动设置时,没有可用的HTML属性来指定它%29。

如果您正在使用jQuery,这样的内容就足够了:

代码语言:javascript
复制
$('.your-checkbox').prop('indeterminate', true)

收音机

代码语言:javascript
复制
<label class="custom-control custom-radio">
  <input id="radio1" name="radio" type="radio" class="custom-control-input">
  <span class="custom-control-indicator"></span>
  <span class="custom-control-description">Toggle this custom radio</span>
</label>
<label class="custom-control custom-radio">
  <input id="radio2" name="radio" type="radio" class="custom-control-input">
  <span class="custom-control-indicator"></span>
  <span class="custom-control-description">Or toggle this other custom radio</span>
</label>

残废

也可以禁用自定义复选框和收音机。添加disabled属性的布尔属性。<input>并且,自定义指示器和标签描述将自动样式化。

代码语言:javascript
复制
<label class="custom-control custom-checkbox">
  <input type="checkbox" class="custom-control-input" disabled>
  <span class="custom-control-indicator"></span>
  <span class="custom-control-description">Check this custom checkbox</span>
</label>

<label class="custom-control custom-radio">
  <input id="radio3" name="radioDisabled" type="radio" class="custom-control-input" disabled>
  <span class="custom-control-indicator"></span>
  <span class="custom-control-description">Toggle this custom radio</span>
</label>

堆叠

自定义复选框和收音机是内联启动。添加具有类的父级.custom-controls-stacked以确保每个窗体控件位于单独的行上。

代码语言:javascript
复制
<div class="custom-controls-stacked">
  <label class="custom-control custom-radio">
    <input id="radioStacked3" name="radio-stacked" type="radio" class="custom-control-input">
    <span class="custom-control-indicator"></span>
    <span class="custom-control-description">Toggle this custom radio</span>
  </label>
  <label class="custom-control custom-radio">
    <input id="radioStacked4" name="radio-stacked" type="radio" class="custom-control-input">
    <span class="custom-control-indicator"></span>
    <span class="custom-control-description">Or toggle this other custom radio</span>
  </label>
</div>

选择菜单

习俗<select>菜单只需要一个自定义类,.custom-select以触发自定义样式。

代码语言:javascript
复制
<select class="custom-select">
  <option selected>Open this select menu</option>
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
</select>

文件浏览器

文件输入是其中最粗糙的,如果您想将它们与函数连接起来,则需要额外的JavaScript。选择文件…和选定的文件名文本。

代码语言:javascript
复制
<label class="custom-file">
  <input type="file" id="file2" class="custom-file-input">
  <span class="custom-file-control"></span>
</label>

下面是它的工作原理:

  • 我们把<input>在...<label>因此,自定义控件正确地触发文件浏览器。
  • 我们隐藏默认文件<input>通孔opacity...
  • 我们用::after生成自定义背景和指令%28选择文件…29%。
  • 我们用::before若要生成并定位浏览纽扣。
  • 我们宣布height<input>为周围内容的适当间隔。

换句话说,它是一个完全自定义的元素,都是通过CSS生成的。

翻译或自定义字符串

:lang()伪类用于方便地将“浏览”和“选择文件…”文本翻译成其他语言。简单地重写或添加条目到$custom-file-text相关的SCSS变量语言标记和本地化字符串。英文字符串也可以用同样的方式定制。例如,下面是如何添加西班牙语翻译%28西班牙语代码的方法es29%:

代码语言:javascript
复制
$custom-file-text: (
  placeholder: (
    en: "Choose file...",
    es: "Seleccionar archivo..."
  ),
  button-label: (
    en: "Browse",
    es: "Navegar"
  )
);

您需要正确设置文档%28或其子树%29的语言,以便显示正确的文本。这可以使用lang属性或者Content-Languagehttp报头,以及其他方法。

代码语言:txt
复制
 © 2011–2017 Twitter, Inc.

©2011-2017自带作者

根据麻省理工学院的许可授权的代码。

根据CreativeCommonsAttributionLicense v3.0授权的文档。

扫码关注腾讯云开发者

领取腾讯云代金券