1. 程式人生 > >bootstrap3.0教程之表單(form)使用詳解

bootstrap3.0教程之表單(form)使用詳解

本文主要講解的是表單,這個其實對於做過網站的人來說,並不陌生,而且可以說是最為常用的提交資料的Form表單。本文主要來講解一下內容:

1.基本案例

2.內聯表單

3.水平排列的表單

4.被支援的控制元件

5.靜態控制元件

6.控制元件狀態

7.控制元件尺寸

8.幫助文字

9.總結

基本案例
單獨的表單控制元件會被自動賦予一些全域性樣式。所有設定了.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>




兩個文字框的寬度的確為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>


水平排列的表單

 通過為表單新增.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>



被支援的控制元件

在表單佈局案例中展示了其所支援的標準表單控制元件。

Input
大部分表單控制元件、文字輸入域控制元件。包括HTML5支援的所有型別:text、password、datetime、datetime-local、date、month、time、week、number、email、url、search、tel和color。

注意:有正確設定了type的input控制元件才能被賦予正確的樣式。

文字框示例

複製程式碼 程式碼如下:
<input type="text" class="form-control" placeholder="Text input">


Textarea

支援多行文字的表單控制元件。可根據需要改變rows屬性。

複製程式碼 程式碼如下:
<h1>textarea</h1>
<textarea class="form-control" rows="3"></textarea>

Checkbox 和 radio

Checkbox用於選擇列表中的一個或多個選項,而radio用於從多個選項中只選擇一個。

預設外觀(堆疊在一起)

複製程式碼 程式碼如下:
<div class="checkbox">
<label>
<input type="checkbox" value="">
Option one is this and that&mdash;be sure to include why it's great
</label>
</div></p>< p><div class="radio">
<label>
<input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" checked>
Option one is this and that&mdash;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控制元件上,可以使這些控制元件排列在一行。

複製程式碼 程式碼如下:
<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

複製程式碼 程式碼如下:
<select class="form-control">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select></p>< p><select multiple class="form-control">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>


靜態控制元件
在水平佈局的表單中,如果需要將一行純文字放置於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 protected]</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樣式。

複製程式碼 程式碼如下:
<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對錶單控制元件的校驗狀態,如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>


控制元件尺寸

通過.input-lg之類的class可以為控制元件設定高度,通過.col-lg-*之類的class可以為控制元件設定寬度。

高度尺寸
建立大一些或小一些的表單控制元件以匹配按鈕尺寸。

複製程式碼 程式碼如下:
<inputclass="form-controlinput-lg"type="text"placeholder=".input-lg">
<inputclass="form-control"type="text"placeholder="Defaultinput">
<inputclass="form-controlinput-sm"type="text"placeholder=".input-sm"></p>< p><selectclass="form-controlinput-lg">...</select>
<selectclass="form-control">...</select>
<selectclass="form-controlinput-sm">...</select>


調整列尺寸
用柵格系統中的列包裹input或其任何父元素,都可很容易的為其設定寬度。

複製程式碼 程式碼如下:
<divclass="row">
<divclass="col-xs-2">
<inputtype="text"class="form-control"placeholder=".col-xs-2">
</div>
<divclass="col-xs-3">
<inputtype="text"class="form-control"placeholder=".col-xs-3">
</div>
<divclass="col-xs-4">
<inputtype="text"class="form-control"placeholder=".col-xs-4">
</div>
</div>


幫助文字
用於表單控制元件的塊級幫助文字。

複製程式碼 程式碼如下:
<spanclass="help-block">自己獨佔一行或多行的塊級幫助文字。</span>

相關推薦

bootstrap3.0教程(form)使用

本文主要講解的是表單,這個其實對於做過網站的人來說,並不陌生,而且可以說是最為常用的提交資料的Form表單。本文主要來講解一下內容: 1.基本案例 2.內聯表單 3.水平排列的表單 4.被支援的控制元件 5.靜態控制元件 6.控制元件狀態 7.控制元件尺寸 8.幫助文字 9

HTML5-從0開始學習屬性

什麼是表單? 表單是網頁中資料採集的工具。 表單組成部分由三部分組成: (一)表單標籤<form> (二)表單域<input> (三)表單按鈕<button> 現在我們來逐步解釋這三部分。 (一)表單標籤<form>

微信小程式基礎Form的使用

表單Form的應用很廣泛,我們可以利用form設計登入註冊,也可以設計一種答題問卷的形式,今天主要講一下form的使用form表單,將元件內輸入的"switch","input","checkbox","slider","radio","picker"的值進行提交,資料的格式

雲客Drupal8原始碼分析Form API

在閱讀本主題前建議你先閱讀本系列前面的《表單定義示例》主題,看一看在drupal8中是如何運用表單的。表單處理流程:一般情況下表單流程是先顯示一個表單,使用者填寫,然後提交,系統處理,如果有錯則重新顯示並給出錯誤提示,反之沒有錯誤那麼完成後給出一個響應或者一個重定向響應,這是

HTMLform的select標籤

form標籤中,以下標籤實現表單元素新增: <input>表單輸入標籤 <select>下拉選單和列表標籤 <option>下拉選單和列表專案標籤 <textarea>文字域標籤 <optgroup>下拉選單和列表專

【vue】vee-validate 驗證

Pre:安裝   npm install [email protected]   內建的校驗規則 after{target} - 比target要大的一個合法日期,格式(DD/MM/YYYY) alpha - 只包

MYSQL資料庫----的操作

此篇主要介紹資料庫中表的操作。資料庫是表的容器,表,必須輸入某個資料庫,因此在建立表之前要指明資料庫。1.表的建立列定義: 列名   列的資料型別   [列的屬性(約束)]建立表的SQL命令:create table 表名(列結構) [表選項];查看錶的定義,可以用DESCR

《angular2入門系列實踐》——驗證

背景:      最近在itoo頁面調整的時候,發現頁面表單或者是文字框沒有做基本的判斷操作,所以著手demo一篇,希望對大家有幫助!! 1.建立表單元件: ng g c login

javascript 指令碼

表單的基礎知識 在HTML中,表單是由<form>元素來組成的。在js中,表單對應的則是HTMLFormElement型別。它和其他HTML元素一樣具有相同的預設屬性。下面是HTMLFormElement獨有的屬性和方法: acceptCharset:伺服器能夠

zabbix3.0server端部署

server 安裝 zabbix yum 配置 下載yum配置rpm -ivh http://mirrors.aliyun.com/zabbix/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch.rpm 安裝依賴包[[email

easyui 驗證

pla focus res default code font led disable function 1 /** 2 * 擴展easyui的validator插件rules,支持更多類型驗證 3 */ 4 $.extend($.fn.val

關於form元素中onsubmit事件處理機制的認識

讓我 clas 教程 是否 它的 默認方法 對象實例 action 事件處理機制   博主目前處於Js學習的初期,遇到了很多問題,比如今天的關於表單form元素中onsubmit事件問題,根據教程所述,onsubmit事件是在表單提交的時候觸發的,但是我看到教程上的onsu

Bootstrap中水平排列的form-inline

name 記住密碼 3.0 lac type link form boot 排列 1 <html> 2 <head> 3 <title>初識Bootstrap</title> 4 <meta charset="

Djangoform ajax應用(上)

ajax django 表單 一、項目說明 學習django版本1.8.2,把之前零散學習的知識整合下,主要涉及到:項目開始,ajax數據調用,註冊,數據錄入,數據修改,數據刪除,數據建模等完成一個完整的前後臺功能簡單的web。數據庫默認用sqlite1、創建djano項目: $

MySQL(九)數據的查詢(SELECT語法)二

clas reg 3.2 查詢語句 我們 lin where 過濾 情況 上一篇講了比較簡單的單表查詢以及MySQL的組函數,這一篇給大家分享一點比較難得知識了,關於多表查詢,子查詢,左連接,外連接等等。希望大家能都得到幫助! 在開始之前因為要多表查詢,所以搭建好環境:

jQuery選擇器元素選擇器

padding ima col image char ref 選中 doc 技術分享 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" conte

自定義流程gooflow2.0+自定義

log ges bapi cnblogs 語句 參與者 源碼 -1 通過 一、功能簡介 gooflow功能清單1、自定義流程繪制2、自定義屬性添加3、支持3種步驟類型普通審批步驟自動決策步驟手動決策步驟 4、決策方式(支持js決策,sql語句決策) 5、審批人員參與方

(十二)easyUI和驗證完成登錄頁面

() 成功 options 表單提交 odi 1-1 java ima 1.4 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

php-2(驗證)

eth left cit span dem 讓我 但是 cape ren PHP 表單驗證 本章節我們將介紹如何使用PHP驗證客戶端提交的表單數據。 PHP 表單驗證 在處理PHP表單時我們需要考慮安全性。 本章節我們將展示PHP表單數據安全處理

Framework7學習筆記

選項 href docs action put password ide tor XML 一:表單布局基本格式 表單布局是通過列表來實現的。 <div class="list-block"> <ul> <!--一個表單