Bootstrap 4 อินพุตแบบฟอร์ม
การควบคุมแบบฟอร์มที่รองรับ
Bootstrap รองรับการควบคุมแบบฟอร์มต่อไปนี้:
- ป้อนข้อมูล
- พื้นที่ข้อความ
- ช่องทำเครื่องหมาย
- วิทยุ
- เลือก
อินพุตบูตสแตรป
Bootstrap รองรับประเภทอินพุต HTML5 ทั้งหมด: ข้อความ, รหัสผ่าน, วันที่เวลา, datetime-local, วันที่, เดือน, เวลา, สัปดาห์, หมายเลข, อีเมล, url, ค้นหา, โทรและสี
หมายเหตุ:อินพุตจะไม่ได้รับการจัดรูปแบบอย่างสมบูรณ์หากประเภทของอินพุตไม่ได้รับการประกาศอย่างถูกต้อง!
ตัวอย่างต่อไปนี้ประกอบด้วยองค์ประกอบอินพุตสององค์ประกอบ หนึ่งในtype="text"
และหนึ่งใน
type="password"
. ดังที่เราได้กล่าวไว้ในบทของฟอร์ม เราใช้.form-control
คลาสเพื่อจัดรูปแบบอินพุตด้วยความกว้างเต็มและการเติมที่เหมาะสม ฯลฯ:
ตัวอย่าง
<div class="form-group">
<label for="usr">Name:</label>
<input type="text" class="form-control" id="usr">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd">
</div>
Bootstrap Textarea
ตัวอย่างต่อไปนี้มีพื้นที่ข้อความ:
ตัวอย่าง
<div class="form-group">
<label for="comment">Comment:</label>
<textarea class="form-control" rows="5" id="comment"></textarea>
</div>
Bootstrap ช่องทำเครื่องหมาย
ช่องทำเครื่องหมายจะใช้หากคุณต้องการให้ผู้ใช้เลือกตัวเลือกจำนวนเท่าใดก็ได้จากรายการตัวเลือกที่ตั้งไว้ล่วงหน้า
ตัวอย่างต่อไปนี้มีช่องทำเครื่องหมายสามช่อง ตัวเลือกสุดท้ายถูกปิดใช้งาน:
ตัวอย่าง
<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">
<label class="form-check-label">
<input type="checkbox" class="form-check-input" value="" disabled>Option 3
</label>
</div>
ตัวอย่างอธิบาย
ใช้องค์ประกอบเสื้อคลุมด้วยclass="form-check"
เพื่อให้แน่ใจว่ามีระยะขอบที่เหมาะสมสำหรับป้ายกำกับและช่องทำเครื่องหมาย
เพิ่ม.form-check-label
คลาสเพื่อติดป้ายกำกับองค์ประกอบ และ.form-check-input
กำหนดสไตล์กล่องกาเครื่องหมายภายใน.form-check
คอนเทนเนอร์อย่างเหมาะสม
ช่องทำเครื่องหมายแบบอินไลน์
ใช้.form-check-inline
ชั้นเรียนถ้าคุณต้องการให้ช่องทำเครื่องหมายปรากฏในบรรทัดเดียวกัน:
ตัวอย่าง
<div class="form-check-inline">
<label class="form-check-label">
<input type="checkbox" class="form-check-input" value="">Option 1
</label>
</div>
<div class="form-check-inline">
<label
class="form-check-label">
<input type="checkbox"
class="form-check-input" value="">Option 2
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label">
<input type="checkbox" class="form-check-input" value="" disabled>Option 3
</label>
</div>
ปุ่มตัวเลือก Bootstrap
ปุ่มตัวเลือกจะใช้หากคุณต้องการจำกัดผู้ใช้ให้เหลือเพียงตัวเลือกเดียวจากรายการตัวเลือกที่ตั้งไว้ล่วงหน้า
ตัวอย่างต่อไปนี้มีปุ่มตัวเลือกสามปุ่ม ตัวเลือกสุดท้ายถูกปิดใช้งาน:
ตัวอย่าง
<div class="form-check">
<label class="form-check-label">
<input type="radio" class="form-check-input"
name="optradio">Option 1
</label>
</div>
<div class="form-check">
<label
class="form-check-label">
<input type="radio"
class="form-check-input" name="optradio">Option 2
</label>
</div>
<div class="form-check disabled">
<label class="form-check-label">
<input type="radio" class="form-check-input" name="optradio" disabled>Option 3
</label>
</div>
เช่นเดียวกับช่องทำเครื่องหมาย ใช้.form-check-inline
คลาสถ้าคุณต้องการให้ปุ่มตัวเลือกปรากฏในบรรทัดเดียวกัน:
ตัวอย่าง
<div class="form-check-inline">
<label class="form-check-label">
<input type="radio" class="form-check-input" name="optradio">Option 1
</label>
</div>
<div class="form-check-inline">
<label
class="form-check-label">
<input type="radio"
class="form-check-input" name="optradio">Option 2
</label>
</div>
<div class="form-check-inline disabled">
<label class="form-check-label">
<input type="radio" class="form-check-input" name="optradio" disabled>Option 3
</label>
</div>
Bootstrap เลือกรายการ
รายการที่เลือกจะใช้หากคุณต้องการให้ผู้ใช้เลือกจากหลายตัวเลือก
ตัวอย่างต่อไปนี้มีรายการดรอปดาวน์ (เลือกรายการ):
ตัวอย่าง
<div class="form-group">
<label for="sel1">Select list:</label>
<select class="form-control" id="sel1">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
</div>
ขนาดการควบคุมแบบฟอร์ม
เปลี่ยนขนาดของตัวควบคุมฟอร์มด้วย.form-control-sm
or .form-control-lg
:
ตัวอย่าง
<input type="text" class="form-control form-control-sm">
<input type="text" class="form-control form-control">
<input type="text" class="form-control form-control-lg">
การควบคุมแบบฟอร์มด้วยข้อความธรรมดา
ใช้.form-control-plaintext
หากคุณต้องการจัดรูปแบบฟิลด์อินพุตเป็นข้อความธรรมดา:
ตัวอย่าง
<input type="text" class="form-control-plaintext">
ไฟล์ควบคุมฟอร์มและช่วง
เพิ่ม.form-control-range
คลาสไปที่input type"range"
หรือ.form-control-file
เพื่อinput type"file"
กำหนดสไตล์ตัวควบคุมช่วงหรือฟิลด์ไฟล์ที่มีความกว้างเต็ม:
ตัวอย่าง
<input type="range" class="form-control-range">
<input type="file"
class="form-control-file border">