Bootstrap來(lái)自 Twitter,是目前最受歡迎的前端框架。Bootstrap 是基于 HTML、CSS、JAVASCRIPT 的,它簡(jiǎn)潔靈活,使得 Web 開(kāi)發(fā)更加快捷。本文介紹一下Bootstrap的表單類型以及實(shí)現(xiàn)方法。
Bootstrap 提供了下列類型的表單布局:
- 垂直表單(默認(rèn))
- 內(nèi)聯(lián)表單
- 水平表單
- 垂直或基本表單
基本的表單結(jié)構(gòu)是 Bootstrap 自帶的,個(gè)別的表單控件自動(dòng)接收一些全局樣式。
下面列出了創(chuàng)建基本表單的步驟:
向父 <form> 元素添加 role="form"。
把標(biāo)簽和控件放在一個(gè)帶有 class .form-group 的 <div> 中。這是獲取最佳間距所必需的。
向所有的文本元素 <input>、<textarea> 和 <select> 添加 class .form-control。
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 實(shí)例 - 基本表單</title>
<link rel="stylesheet" href="http://apps./libs/bootstrap/3.3.0/css/bootstrap.min.css">
<script src="http://apps./libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://apps./libs/bootstrap/3.3.0/js/bootstrap.min.js"></script>
</head>
<body>
<form role="form">
<div class="form-group">
<label for="name">名稱</label>
<input type="text" class="form-control" id="name"
placeholder="請(qǐng)輸入名稱">
</div>
<div class="form-group">
<label for="inputfile">文件輸入</label>
<input type="file" id="inputfile">
<p class="help-block">這里是塊級(jí)幫助文本的實(shí)例。</p>
</div>
<div class="checkbox">
<label>
<input type="checkbox"> 請(qǐng)打勾
</label>
</div>
<button type="submit" class="btn btn-default">提交</button>
</form>
</body>
</html>
- 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
- 29
- 30
- 31
- 32
- 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
- 29
- 30
- 31
- 32
結(jié)果如下所示:

內(nèi)聯(lián)表單
如果需要?jiǎng)?chuàng)建一個(gè)表單,它的所有元素是內(nèi)聯(lián)的,向左對(duì)齊的,標(biāo)簽是并排的,請(qǐng)向 <form> 標(biāo)簽添加 class .form-inline。
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 實(shí)例 - 內(nèi)聯(lián)表單</title>
<link rel="stylesheet" href="http://apps./libs/bootstrap/3.3.0/css/bootstrap.min.css">
<script src="http://apps./libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://apps./libs/bootstrap/3.3.0/js/bootstrap.min.js"></script>
</head>
<body>
<form class="form-inline" role="form">
<div class="form-group">
<label class="sr-only" for="name">名稱</label>
<input type="text" class="form-control" id="name"
placeholder="請(qǐng)輸入名稱">
</div>
<div class="form-group">
<label class="sr-only" for="inputfile">文件輸入</label>
<input type="file" id="inputfile">
</div>
<div class="checkbox">
<label>
<input type="checkbox"> 請(qǐng)打勾
</label>
</div>
<button type="submit" class="btn btn-default">提交</button>
</form>
</body>
</html>
- 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
- 29
- 30
- 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
- 29
- 30
結(jié)果如下所示:

默認(rèn)情況下,Bootstrap 中的 input、select 和 textarea 有 100% 寬度。在使用內(nèi)聯(lián)表單時(shí),您需要在表單控件上設(shè)置一個(gè)寬度。
使用 class .sr-only,您可以隱藏內(nèi)聯(lián)表單的標(biāo)簽。
水平表單
水平表單與其他表單不僅標(biāo)記的數(shù)量上不同,而且表單的呈現(xiàn)形式也不同。如需創(chuàng)建一個(gè)水平布局的表單,請(qǐng)按下面的幾個(gè)步驟進(jìn)行:
向父 <form> 元素添加 class .form-horizontal。
把標(biāo)簽和控件放在一個(gè)帶有 class .form-group 的 <div> 中。
向標(biāo)簽添加 class .control-label。
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 實(shí)例 - 水平表單</title>
<link rel="stylesheet" href="http://apps./libs/bootstrap/3.3.0/css/bootstrap.min.css">
<script src="http://apps./libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://apps./libs/bootstrap/3.3.0/js/bootstrap.min.js"></script>
</head>
<body>
<form class="form-horizontal" role="form">
<div class="form-group">
<label for="firstname" class="col-sm-2 control-label">名字</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="firstname"
placeholder="請(qǐng)輸入名字">
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-sm-2 control-label">姓</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="lastname"
placeholder="請(qǐng)輸入姓">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label>
<input type="checkbox"> 請(qǐng)記住我
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">登錄</button>
</div>
</div>
</form>
</body>
</html>
- 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
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 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
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
結(jié)果如下所示:

本文介紹了Bootstrap的表單類型和實(shí)現(xiàn)方法,下篇介紹表單支持的控件。
|