ซีเลกเตอร์ input
ซีเลกเตอร์ :input เลือกองค์ประกอบควบคุม
ฟอร์ม - อินพุต, เท็กซ์เอเรีย, รายการดรอปดาวน์ และ
ปุ่ม ดูแท็ก:
input,
textarea,
button,
select.
เนื่องจาก :input ไม่ได้อยู่ในข้อกำหนด CSS
ดังนั้นเพื่อประสิทธิภาพที่ดีขึ้นในเบราว์เซอร์สมัยใหม่
ควรกรององค์ประกอบก่อนโดยใช้ CSS selector ล้วน แล้ว
จึงใช้ .filter(':input').
ไวยากรณ์
นี่คือวิธีการเลือกองค์ประกอบควบคุมฟอร์ม:
$(':input');
ตัวอย่าง
ตามทฤษฎีข้างต้น
ลองเลือกองค์ประกอบควบคุมฟอร์มทั้งหมดและแสดง
จำนวนของพวกมันในคอนโซลโดยใช้คุณสมบัติ
length เรา
จะเห็นว่ามีแท็กทั้งหมด 13 แท็ก:
<form>
<input type="button" value="button">
<input type="checkbox">
<input type="file">
<input type="hidden">
<input type="image">
<input type="password">
<input type="radio">
<input type="reset">
<input type="submit">
<input type="text">
<select>
<option>option</option>
</select>
<textarea></textarea>
<button>button</button>
</form>
textarea {
height: 25px;
}
let allInputs = $(':input');
console.log('จำนวนแท็ก: ' + allInputs.length);
$('form').submit(function(event) {
event.preventDefault(); // ป้องกันการส่งฟอร์ม
});