Bootstrap4 支持以下表單控件:
input
textarea
checkbox
radio
select
Bootstrap 支持所有的 HTML5 輸入類型: text, password, datetime, datetime-local, date, month, time, week, number, email, url, search, tel, 以及 color。
注意:: 如果 input 的 type 屬性未正確聲明,輸入框的樣式將不會顯示。
以下示例使用兩個 input 元素,一個是 text,一個是 password :
<!DOCTYPE html> <html> <head> <title>Bootstrap 示例</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>表單控件: input</h2> <p>以下示例使用兩個 input 元素,一個是 text,一個是 password :</p> <form> <div class="form-group"> <label for="usr">用戶名:</label> <input type="text" class="form-control" id="usr"> </div> <div class="form-group"> <label for="pwd">密碼:</label> <input type="password" class="form-control" id="pwd"> </div> </form> </div> </body> </html>測試看看 ?/?
以下示例演示了 textarea 的樣式。
<!DOCTYPE html> <html> <head> <title>Bootstrap 示例</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>表單控件: textarea</h2> <p>以下示例演示了 textarea 的樣式。</p> <form> <div class="form-group"> <label for="comment">評論:</label> <textarea class="form-control" rows="5" id="comment"></textarea> </div> </form> </div> </body> </html>測試看看 ?/?
復(fù)選框用于讓用戶從一系列預(yù)設(shè)置的選項(xiàng)中進(jìn)行選擇,可以選一個或多個。
以下示例包含了三個選項(xiàng)。最后一個是禁用的:
<!DOCTYPE html> <html> <head> <title>Bootstrap 示例</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>表單控件: checkbox</h2> <p>以下示例包含了三個選項(xiàng)。最后一個是禁用的:</p> <form> <div class="form-check"> <label class="form-check-label"> <input type="checkbox" class="form-check-input" value="">Option 1 </label> </div> <div class="form-check"> <label class="form-check-label"> <input type="checkbox" class="form-check-input" value="">Option 2 </label> </div> <div class="form-check disabled"> <label class="form-check-label"> <input type="checkbox" class="form-check-input" value="" disabled>Option 3 </label> </div> </form> </div> </body> </html>測試看看 ?/?
使用 .form-check-inline 類可以讓選項(xiàng)顯示在同一行上:
<!DOCTYPE html> <html> <head> <title>Bootstrap 示例</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>表單控件: checkbox</h2> <p>以下示例包含了三個選項(xiàng)。最后一個是禁用的,使用 .form-check-inline 類可以讓選項(xiàng)顯示在同一行上:</p> <form> <div class="form-check form-check-inline"> <label class="form-check-label"> <input type="checkbox" class="form-check-input" value="">Option 1 </label> </div> <div class="form-check form-check-inline"> <label class="form-check-label"> <input type="checkbox" class="form-check-input" value="">Option 2 </label> </div> <div class="form-check form-check-inline disabled"> <label class="form-check-label"> <input type="checkbox" class="form-check-input" value="" disabled>Option 3 </label> </div> </form> </div> </body> </html>測試看看 ?/?
單選框用于讓用戶從一系列預(yù)設(shè)置的選項(xiàng)中進(jìn)行選擇,只能選一個。
以下示例包含了三個選項(xiàng)。最后一個是禁用的:
<!DOCTYPE html> <html> <head> <title>Bootstrap 示例</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>表單控件: radio</h2> <p>以下示例包含了三個選項(xiàng)。最后一個是禁用的:</p> <form> <div class="radio"> <label><input type="radio" name="optradio">Option 1</label> </div> <div class="radio"> <label><input type="radio" name="optradio">Option 2</label> </div> <div class="radio disabled"> <label><input type="radio" name="optradio" disabled>Option 3</label> </div> </form> </div> </body> </html>測試看看 ?/?
使用 .radio-inline 類可以讓選項(xiàng)顯示在同一行上:
<!DOCTYPE html> <html> <head> <title>Bootstrap 示例</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>表單控件: radio</h2> <p>以下示例包含了三個選項(xiàng)。最后一個是禁用的,使用 .radio-inline 類可以讓選項(xiàng)顯示在同一行上::</p> <form> <label class="radio-inline"><input type="radio" name="optradio">Option 1</label> <label class="radio-inline"><input type="radio" name="optradio">Option 2</label> <label class="radio-inline"><input type="radio" name="optradio" disabled>Option 3</label> </form> </div> </body> </html>測試看看 ?/?
當(dāng)您想讓用戶從多個選項(xiàng)中進(jìn)行選擇,但是默認(rèn)情況下只能選擇一個選項(xiàng)時,則使用選擇框。
以下示例包含了兩個下拉菜單:
<!DOCTYPE html> <html> <head> <title>Bootstrap 示例</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>表單控件: select</h2> <p>以下表單包含了兩個下拉菜單 (select 列表):</p> <form> <div class="form-group"> <label for="sel1">單選下拉菜單:</label> <select class="form-control" id="sel1"> <option>1</option> <option>2</option> <option>3</option> <option>4</option> </select> <br> <label for="sel2">多選下拉菜單(按住 shift 鍵,可以選取多個選項(xiàng)):</label> <select multiple class="form-control" id="sel2"> <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> </select> </div> </form> </div> </body> </html>測試看看 ?/?