Bootstrap每天必学之表单


Posted in Javascript onNovember 23, 2015

本文主要讲解的是表单,这个其实对于做过网站的人来说,并不陌生,而且可以说是最为常用的提交数据的Form表单。本文主要来讲解一下内容:

1.基本案例
2.内联表单
3.水平排列的表单
4.被支持的控件
5.静态控件
6.控件状态
7.控件尺寸
8.帮助文本

基本案例
 单独的表单控件会被自动赋予一些全局样式。所有设置了.form-control的<input>、<textarea>和<select>元素都将被默认设置为width: 100%;。将label和前面提到的这些控件包裹在.form-group中可以获得最好的排列。

<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>

Bootstrap每天必学之表单

两个文本框的宽度的确为100%。并且有三个form-group。
内联表单
为左对齐和inline-block级别的控件设置.form-inline,可以将其排布的更紧凑。
需要设置宽度:在Bootstrap中,input、select和textarea默认被设置为100%宽度。为了使用内联表单,你需要专门为使用到的表单控件设置宽度。

 一定要设置label:如果你没有为每个输入控件设置label,屏幕阅读器将无法正确识读。对于这些内联表单,你可以通过为label设置.sr-only已将其隐藏。

<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>

Bootstrap每天必学之表单

水平排列的表单
 通过为表单添加.form-horizontal,并使用Bootstrap预置的栅格class可以将label和控件组水平并排布局。这样做将改变.form-group的行为,使其表现为栅格系统中的行(row),因此就无需再使用.row了。

<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>

Bootstrap每天必学之表单

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

<input type="text" class="form-control" placeholder="Text input">

Bootstrap每天必学之表单

Textarea
支持多行文本的表单控件。可根据需要改变rows属性。  

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

Bootstrap每天必学之表单

Checkbox 和 radio
Checkbox用于选择列表中的一个或多个选项,而radio用于从多个选项中只选择一个。
默认外观(堆叠在一起)

<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>

Bootstrap每天必学之表单 

Inline checkboxes

通过将.checkbox-inline 或 .radio-inline应用到一系列的checkbox或radio控件上,可以使这些控件排列在一行。

<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>

 Bootstrap每天必学之表单

 同理Radio是一样的,只需要添加一下样式即可。
Select

<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>

Bootstrap每天必学之表单 

静态控件
 在水平布局的表单中,如果需要将一行纯文本放置于label的同一行,为<p>元素添加.form-control-static即可。

<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>

Bootstrap每天必学之表单

控件状态

通过为控件和label设置一些基本状态,可以为用户提供回馈。

输入焦点

我们移除了某些表单控件的默认outline样式,并对其:focus状态赋予了box-shadow样式。

<input class="form-control" id="focusedInput" type="text" value="This is focused...">

被禁用的输入框
 

为输入框设置disabled属性可以防止用户输入,并能改变一点外观,使其更直观。

<input class="form-control" id="disabledInput" type="text" placeholder="Disabled input here..." disabled>

被禁用的fieldset

为<fieldset>设置disabled属性可以禁用<fieldset>中包含的所有控件。
<a>标签的链接功能不受影响

这个class只改变<a class="btn btn-default">按钮的外观,并不能禁用其功能。建议自己通过JavaScript代码禁用链接功能。

跨浏览器兼容性

虽然Bootstrap会将这些样式应用到所有浏览器上,Internet Explorer 9及以下浏览器中的<fieldset>并不支持disabled属性。因此建议在这些浏览器上通过JavaScript代码来禁用fieldset

<form role="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="checkbox">
 <label>
 <input type="checkbox"> Can't check this
 </label>
 </div>
 <button type="submit" class="btn btn-primary">Submit</button>
 </fieldset>
</form>

Bootstrap每天必学之表单

可将鼠标移到各个控件上进行查看效果。
校验状态
Bootstrap对表单控件的校验状态,如error、warning和success状态,都定义了样式。使用时,添加.has-warning、.has-error或.has-success到这些控件的父元素即可。任何包含在此元素之内的.control-label、.form-control和.help-block都将接受这些校验状态的样式。

<div class="form-group has-success">
 <label class="control-label" for="inputSuccess">Input with success</label>
 <input type="text" class="form-control" id="inputSuccess">
</div>
<div class="form-group has-warning">
 <label class="control-label" for="inputWarning">Input with warning</label>
 <input type="text" class="form-control" id="inputWarning">
</div>
<div class="form-group has-error">
 <label class="control-label" for="inputError">Input with error</label>
 <input type="text" class="form-control" id="inputError">
</div>

Bootstrap每天必学之表单

控件尺寸
通过.input-lg之类的class可以为控件设置高度,通过.col-lg-*之类的class可以为控件设置宽度。
高度尺寸
创建大一些或小一些的表单控件以匹配按钮尺寸。

<input class="form-control input-lg" type="text" placeholder=".input-lg">
 <input class="form-control" type="text" placeholder="Default input">
 <input class="form-control input-sm" type="text" placeholder=".input-sm">
 
 <select class="form-control input-lg">...</select>
 <select class="form-control">...</select>
 <select class="form-control input-sm">...</select>

Bootstrap每天必学之表单 

调整列尺寸
用栅格系统中的列包裹input或其任何父元素,都可很容易的为其设置宽度。

<div class="row">
 <div class="col-xs-2">
 <input type="text" class="form-control" placeholder=".col-xs-2">
 </div>
 <div class="col-xs-3">
 <input type="text" class="form-control" placeholder=".col-xs-3">
 </div>
 <div class="col-xs-4">
 <input type="text" class="form-control" placeholder=".col-xs-4">
 </div>
</div>

帮助文本
 用于表单控件的块级帮助文本。

<span class="help-block">自己独占一行或多行的块级帮助文本。</span>

 本篇文章主要讲解表单中各种控件的样式控制,其中也有看到按钮的简单样式使用,下一篇文章将重点来讲解按钮的样式。

详细内容可以参考:

如果大家还想深入学习,可以点击这里进行学习,再为大家附两个精彩的专题:Bootstrap学习教程 Bootstrap实战教程

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
JS动态显示表格上下frame的方法
Mar 31 Javascript
jQuery基于muipicker实现仿ios时间选择
Feb 22 Javascript
jQuery基于扩展简单实现倒计时功能的方法
May 14 Javascript
Document.body.scrollTop的值总为零的快速解决办法
Jun 09 Javascript
js 弹出虚拟键盘修改密码的简单实例
Oct 10 Javascript
js实现点击每个li节点,都弹出其文本值及修改
Dec 15 Javascript
vue实现的下拉框功能示例
Jan 29 Javascript
js实现unicode码字符串与utf8字节数据互转详解
Mar 21 Javascript
react koa rematch 如何打造一套服务端渲染架子
Jun 26 Javascript
vue实现二级导航栏效果
Oct 19 Javascript
纯js实现无缝滚动功能代码实例
Feb 21 Javascript
JavaScript鼠标悬停事件用法解析
May 15 Javascript
jquery制作属于自己的select自定义样式
Nov 23 #Javascript
基于jquery实现省市联动效果
Nov 23 #Javascript
jquery实现加载进度条提示效果
Nov 23 #Javascript
使用jquery实现鼠标滑过弹出更多相关信息层附源码下载
Nov 23 #Javascript
javascript实现uploadify上传格式以及个数限制
Nov 23 #Javascript
Jquery 效果使用详解
Nov 23 #Javascript
JavaScript高级教程5.6之基本包装类型(详细)
Nov 23 #Javascript
You might like
pw的一个放后门的方法分析
2007/10/08 PHP
Sorting Array Values in PHP(数组排序)
2011/09/15 PHP
JavaScript Base64编码和解码,实现URL参数传递。
2006/09/18 Javascript
javascript void(0)的妙用
2009/10/21 Javascript
JavaScript栏目列表隐藏/显示简单实现
2013/04/03 Javascript
[原创]推荐10款最热门jQuery UI框架
2014/08/19 Javascript
浅谈Node.js中的定时器
2015/06/18 Javascript
基于Node.js实现nodemailer邮件发送
2016/01/26 Javascript
第三章之Bootstrap 表格与按钮功能
2016/04/25 Javascript
JS简单判断函数是否存在的方法
2017/02/13 Javascript
Bootstrap栅格系统简单实现代码
2017/03/06 Javascript
原生js实现简单的Ripple按钮实例代码
2017/03/24 Javascript
node.js到底要不要加分号浅析
2018/07/11 Javascript
原生JS实现的放大镜特效示例【测试可用】
2018/12/08 Javascript
vue 组件内获取actions的response方式
2019/11/08 Javascript
vue动态循环出的多个select出现过的变为disabled(实例代码)
2019/11/10 Javascript
vue 实现LED数字时钟效果(开箱即用)
2019/12/08 Javascript
使用BeautifulSoup爬虫程序获取百度搜索结果的标题和url示例
2014/01/19 Python
Python操作sqlite3快速、安全插入数据(防注入)的实例
2014/04/26 Python
Python如何读取MySQL数据库表数据
2017/03/11 Python
批量获取及验证HTTP代理的Python脚本
2017/04/23 Python
python利用OpenCV2实现人脸检测
2020/04/16 Python
利用Python yagmail三行代码实现发送邮件
2018/05/11 Python
使用memory_profiler监测python代码运行时内存消耗方法
2018/12/03 Python
Python turtle库绘制菱形的3种方式小结
2019/11/23 Python
python中的subprocess.Popen()使用详解
2019/12/25 Python
CSS3改变浏览器滚动条样式
2019/01/04 HTML / CSS
找到您丢失的钥匙、钱包和手机:Tile
2017/05/19 全球购物
为什么需要版本控制?
2013/08/08 面试题
护校行动方案
2014/05/31 职场文书
教师师德师风自我剖析材料
2014/09/29 职场文书
机关干部四风问题自查报告及整改措施
2014/10/26 职场文书
英文慰问信
2015/02/14 职场文书
离职证明范本
2015/06/12 职场文书
动画「半妖的夜叉姬」新BD特典图公开
2022/03/22 日漫
解决vue-router的beforeRouteUpdate不能触发
2022/04/14 Vue.js