本文主要讲解的是表单,这个其实对于做过网站的人来说,并不陌生,而且可以说是最为常用的提交数据的Form表单。本文主要来讲解一下内容:
1.基本案例
2.内联表单
3.水平排列的表单
4.被支持的控件
5.静态控件
6.控件状态
7.控件尺寸
8.帮助文本
基本案例
单独的表单控件会被自动赋予一些全局样式。所有设置了.form-control的、
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <form role= "form" >
<div class = "form-group" >
<label for = "exampleInputEmail1" >Email address</label>
<input type= "email" class = "form-control" id= "exampleInputEmail1" placeholder= "Enter email" >
</div>
<div class = "form-group" >
<label for = "exampleInputPassword1" >Password</label>
<input type= "password" class = "form-control" id= "exampleInputPassword1" placeholder= "Password" >
</div>
<div class = "form-group" >
<label for = "exampleInputFile" >File input</label>
<input type= "file" id= "exampleInputFile" >
<p class = "help-block" >Example block-level help text here.</p>
</div>
<div class = "checkbox" >
<label>
<input type= "checkbox" > Check me out
</label>
</div>
<button type= "submit" class = "btn btn-default" >Submit</button>
</form>
|
登录后复制

两个文本框的宽度的确为100%。并且有三个form-group。
内联表单
为左对齐和inline-block级别的控件设置.form-inline,可以将其排布的更紧凑。
需要设置宽度:在Bootstrap中,input、select和textarea默认被设置为100%宽度。为了使用内联表单,你需要专门为使用到的表单控件设置宽度。
一定要设置label:如果你没有为每个输入控件设置label,屏幕阅读器将无法正确识读。对于这些内联表单,你可以通过为label设置.sr-only已将其隐藏。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <form class = "form-inline" role= "form" >
<div class = "form-group" >
<label class = "sr-only" for = "exampleInputEmail2" >Email address</label>
<input type= "email" class = "form-control" id= "exampleInputEmail2" placeholder= "Enter email" >
</div>
<div class = "form-group" >
<label class = "sr-only" for = "exampleInputPassword2" >Password</label>
<input type= "password" class = "form-control" id= "exampleInputPassword2" placeholder= "Password" >
</div>
<div class = "checkbox" >
<label>
<input type= "checkbox" > Remember me
</label>
</div>
<button type= "submit" class = "btn btn-default" >Sign in</button>
</form>
|
登录后复制

水平排列的表单
通过为表单添加.form-horizontal,并使用Bootstrap预置的栅格class可以将label和控件组水平并排布局。这样做将改变.form-group的行为,使其表现为栅格系统中的行(row),因此就无需再使用.row了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <form class = "form-horizontal" role= "form" >
<div class = "form-group" >
<label for = "inputEmail3" class = "col-sm-2 control-label" >Email</label>
<div class = "col-sm-10" >
<input type= "email" class = "form-control" id= "inputEmail3" placeholder= "Email" >
</div>
</div>
<div class = "form-group" >
<label for = "inputPassword3" class = "col-sm-2 control-label" >Password</label>
<div class = "col-sm-10" >
<input type= "password" class = "form-control" id= "inputPassword3" placeholder= "Password" >
</div>
</div>
<div class = "form-group" >
<div class = "col-sm-offset-2 col-sm-10" >
<div class = "checkbox" >
<label>
<input type= "checkbox" > Remember me
</label>
</div>
</div>
</div>
<div class = "form-group" >
<div class = "col-sm-offset-2 col-sm-10" >
<button type= "submit" class = "btn btn-default" >Sign in</button>
</div>
</div>
</form>
|
登录后复制

被支持的控件
在表单布局案例中展示了其所支持的标准表单控件。
Input
大部分表单控件、文本输入域控件。包括HTML5支持的所有类型:text、password、datetime、datetime-local、date、month、time、week、number、email、url、search、tel和color。
注意:有正确设置了type的input控件才能被赋予正确的样式。
文本框示例

Textarea
支持多行文本的表单控件。可根据需要改变rows属性。
1 2 | <h1>textarea</h1>
<textarea class = "form-control" rows= "3" ></textarea>
|
登录后复制

Checkbox 和 radio
Checkbox用于选择列表中的一个或多个选项,而radio用于从多个选项中只选择一个。
默认外观(堆叠在一起)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <div class = "checkbox" >
<label>
<input type= "checkbox" value= "" >
Option one is this and that—be sure to include why it's great
</label>
</div>
<div class = "radio" >
<label>
<input type= "radio" name= "optionsRadios" id= "optionsRadios1" value= "option1" checked>
Option one is this and that—be sure to include why it's great
</label>
</div>
<div class = "radio" >
<label>
<input type= "radio" name= "optionsRadios" id= "optionsRadios2" value= "option2" >
Option two can be something else and selecting it will deselect option one
</label>
</div>
|
登录后复制
Inline checkboxes
通过将.checkbox-inline 或 .radio-inline应用到一系列的checkbox或radio控件上,可以使这些控件排列在一行。
1 2 3 4 5 6 7 8 9 | <label class = "checkbox-inline" >
<input type= "checkbox" id= "inlineCheckbox1" value= "option1" > 1
</label>
<label class = "checkbox-inline" >
<input type= "checkbox" id= "inlineCheckbox2" value= "option2" > 2
</label>
<label class = "checkbox-inline" >
<input type= "checkbox" id= "inlineCheckbox3" value= "option3" > 3
</label>
|
登录后复制

同理Radio是一样的,只需要添加一下样式即可。
Select
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <select class = "form-control" >
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
<select multiple class = "form-control" >
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
|
登录后复制
静态控件
在水平布局的表单中,如果需要将一行纯文本放置于label的同一行,为
元素添加.form-control-static即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <form class = "form-horizontal" role= "form" >
<div class = "form-group" >
<label class = "col-sm-2 control-label" >Email</label>
<div class = "col-sm-10" >
<p class = "form-control-static" >email@example.com</p>
</div>
</div>
<div class = "form-group" >
<label for = "inputPassword" class = "col-sm-2 control-label" >Password</label>
<div class = "col-sm-10" >
<input type= "password" class = "form-control" id= "inputPassword" placeholder= "Password" >
</div>
</div>
</form>
|
登录后复制

控件状态
通过为控件和label设置一些基本状态,可以为用户提供回馈。
输入焦点
我们移除了某些表单控件的默认outline样式,并对其:focus状态赋予了box-shadow样式。
被禁用的输入框
为输入框设置disabled属性可以防止用户输入,并能改变一点外观,使其更直观。
被禁用的fieldset
为