Bootstrap學習筆記系列3-------Bootstrap簡單表單顯示


表單布局

垂直或基本表單

基本的表單結構時BootStrap自帶的,創建基本表單的步驟如下:

  • 向父<form>元素添加role = “form”
  • 為了獲取最佳的間距,把標簽和控件放在一個div.form-group中,div放在父form下;
  • 向所有的文本元素<input><textarea><select>添加.form-control
<!DOCTYPE html>
<html>
   <head>
      <title>Bootstrap 模板</title>
      <meta charset="utf-8">
      <!-- 引入 Bootstrap -->
      <link href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
   </head>
   <body>

      <form role="form">
         <div class="form-group">
            <label for="name">名稱</label>
            <input type="text" class="form-control" id="name" name="name-text"  placeholder="請輸入你的名稱">
         </div>
      </form>

      <!-- jQuery (Bootstrap 的 JavaScript 插件需要引入 jQuery) -->
      <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
      <!-- 包括所有已編譯的插件 -->
      <script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
   </body>
</html>

添加了form-controlClass的input會和label分為兩行,並且獲得焦點時會有藍色的邊框樣式。

內聯表單

內聯表單中所有的元素都是內聯的,標簽都是並排的

  • <form>標簽中添加classfrom-inline;
  • 每個表單需要被包含在div.form-group,checkbox可以包含在div.checkbox,radio可以包含在div.radio;
  • 默認情況下,bootstrap中的inputselecttextarea有100%寬度,使用內聯表單時,可以設置表單控件的寬度來設置;
  • 對標簽描述添加sr-only可以隱藏標簽描述。
<!DOCTYPE html>
<html>
   <head>
      <title>Bootstrap 模板</title>
      <meta charset="utf-8">
      <!-- 引入 Bootstrap -->
      <link href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
   </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" name="name-text"  placeholder="請輸入你的名稱" style="width: 170px">
         </div>
         <div class="form-group">
            <input type="file" name="inputfile" style="width: 170px">
         </div>
            <label>
            <input type="checkbox" class="checkbox">請打勾            
            </label>            
      </form>

      <!-- jQuery (Bootstrap 的 JavaScript 插件需要引入 jQuery) -->
      <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
      <!-- 包括所有已編譯的插件 -->
      <script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
   </body>
</html>

水平表單

  • 向父<form>添加類form-horizontal;
  • 把標簽和空間放在一個 div,form-group<div>中;
  • 向標簽添加class control-label.
<!DOCTYPE html>
<html>
   <head>
      <title>Bootstrap 模板</title>
      <meta charset="utf-8">
      <!-- 引入 Bootstrap -->
      <link href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
   </head>
   <body>

      <!--父元素添加form-horizontal類-->
      <form class="form-horizontal" role="form">

         <!--標簽和控件放在一個帶有 class .form-group 的 <div> 中-->
         <div class="form-group">

            <!--向標簽添加 class .control-label。-->
            <label for="username" class="col-sm-2 control-label">名字:</label>

            <!--控制表單的寬度-->
            <div class="col-sm-3">
               <input type="text" class="form-control" id="username"  placeholder="請輸入用戶名">
            </div>
         </div>

         <div class="form-group">
            <label for="password" class="col-sm-2 control-label">密碼:</label>
            <div class="col-sm-3">
            <input type="password" class="form-control" id="password" placeholder="請輸入密碼">
            </div>
         </div>

         <div class="form-group">
         <div class="col-sm-offset-2 col-sm-10">
            <div class="checkbox">
               <label>
                  <input type="checkbox">請記住我
               </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>
      
      <!-- jQuery (Bootstrap 的 JavaScript 插件需要引入 jQuery) -->
      <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
      <!-- 包括所有已編譯的插件 -->
      <script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
   </body>
</html>

支持表單的控件

bootstrap支持常見的表單控件,主要是input,textarea,checkbox,radio和select。

輸入框(input)

Bootstrap提供了對所有原生的HTML5的input類型的支持,包括:textpassworddatetimedatetime-localdatemonthtimeweeknumberurlsearchtelcolor

文本框(Textarea)

需要進行多行的輸入時,則可以使用文本框textarea。必要時改變rows屬性(較少的行 = 較少的盒子,較多的行 = 較多的盒子)。

復選框(Checkbox)和單選框(Radio)

  • 創建表單時,如果想讓用戶從列表中選擇若干選項時,請使用checkbox。如果想限制用戶只能選擇一個選項,就使用radion
  • 對於一些列的復選框和單選框使用Checkbox-inline或radio-inlie,控制它們顯示在同一行上。
      <label class="checkbox-inline">
         <input type="radio" name="optionsRadiosinline" id="optionsRaios1" value="option1" >選項1
         </label>
         <label class="checkbox-inline">

<!--設置checked屬性來表示默認被選中-->
         <input type="radio" name="optionsRadiosinline" id="optionsRaios2" value="option2" checked="">選項2
      </label>

選擇框(select)

想讓用戶從多個選項中進行選擇,但是默認的情況下只能選擇一個選項時,則使用選擇框

  • 使用<select>展示列表選項
  • 使用multiple=“multiple”允許用戶選擇多個選項

靜態控件

當在一個水平表單標簽后放置純文本時,在<p>上使用form-control-static

表單控件狀態

  • :focus 狀態(即,用戶點擊 input 或使用 tab 鍵聚焦到 input 上)時,輸入框的輪廓會被移除,同時應用box-shadow。
  • 禁用輸入框input,要想禁用一個輸入框input,只需要簡單的添加disable屬性,就會禁用輸入框,還會改變輸入框的樣式和鼠標懸停的樣式。
  • 禁用字段集fieldset,對<fieldset>添加disable屬性來禁用<fieldset>內的所有控件。
  • 驗證狀態,對父元素
    簡單的添加 has-waringhas-errorhas-success即可使用驗證狀態。
<form class="form-horizontal" role="form">
  <div class="form-group">
    <label for="inputPassword" class="col-sm-2 control-label">禁用</label>
    <div class="col-sm-10">

      <!--通過對<input>標簽簡單的添加disbale屬性,就能禁止該控件-->
      <input class="form-control" id="disabledInput" type="text" placeholder="該輸入框禁止輸入..." disabled>
    </div>
  </div>

  <!--對<fieldset>添加disabled屬性禁用<fieldset>內的所有組件-->
  <fieldset disabled>
    <div class="form-group">
      <label for="disabledTextInput" class="col-sm-2 control-label">禁用輸入(Fieldset disabled)</label>
      <div class="col-sm-10">
        <input type="text" id="disabledTextInput" class="form-control" placeholder="禁止輸入">
      </div>
    </div>
    <div class="form-group">
      <label for="disabledSelect" class="col-sm-2 control-label">禁用選擇菜單(Fieldset disabled)</label>
      <div class="col-sm-10">
        <select id="disabledSelect" class="form-control">
          <option>禁止選擇</option>
        </select>
      </div>
    </div>
  </fieldset>

  <!--對父元素添加has-success類用於成功消息的驗證樣式-->
  <div class="form-group has-success">
    <label class="col-sm-2 control-label" for="inputSuccess">輸入成功</label>
    <div class="col-sm-10">
      <input type="text" class="form-control" id="inputSuccess">
    </div>
  </div>

  <!--對父元素添加has-warning類用於警告消息的驗證樣式-->
  <div class="form-group has-warning">
    <label class="col-sm-2 control-label" for="inputWarning">輸入警告</label>
    <div class="col-sm-10">
      <input type="text" class="form-control" id="inputWarning">
    </div>
  </div>

  <!--對父元素添加has-error類用錯誤消息的驗證樣式-->
  <div class="form-group has-error">
    <label class="col-sm-2 control-label" for="inputError">輸入錯誤</label>
    <div class="col-sm-10">
      <input type="text" class="form-control" id="inputError">
    </div>
  </div>
</form>

表單控件的大小

使用input-lg(lg可以替換成md、sm下同)和col-lg-*來設置表單的高度和寬度。

表單幫助文本

表單控件可以在輸入框上有一個塊級的幫助文本,為了添加一個占用整個寬度的內容快, 在input后使用help-block

      <form role="form">
         <span>幫助文本實例</span>
         <input class="form-control" type="text">
         <p class="help-block">一個較長的文本,超過一行,需要擴展到下一行</p>
      </form>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM