ตัวเลือก Datalist Collection
ตัวอย่าง
ค้นหาว่ามีกี่ตัวเลือกในองค์ประกอบ <datalist> ที่เฉพาะเจาะจง:
var x = document.getElementById("browsers").options.length;
ผลลัพธ์ของxจะเป็น:
5
ตัวอย่าง "ลองใช้เอง" เพิ่มเติมด้านล่าง
ความหมายและการใช้งาน
คอลเลกชันตัวเลือกส่งคืนคอลเลกชันของตัวเลือกทั้งหมดในองค์ประกอบ <datalist>
หมายเหตุ:องค์ประกอบในคอลเลกชันจะถูกจัดเรียงตามที่ปรากฏในซอร์สโค้ด
รองรับเบราว์เซอร์
Collection | |||||
---|---|---|---|---|---|
options | Yes | 10.0 | Yes | Yes | Yes |
ไวยากรณ์
datalistObject.options
คุณสมบัติ
Property | Description |
---|---|
length | Returns the number of <option> elements in the collection. Note: This property is read-only |
วิธีการ
Method | Description |
---|---|
[index] | Returns the <option> element from the collection with the specified index (starts at 0). Note: Returns null if the index number is out of range |
item(index) | Returns the <option> element from the collection with the specified index (starts at 0). Note: Returns null if the index number is out of range |
namedItem(id) | Returns the <option> element from the collection with the specified id. Note: Returns null if the id does not exist |
รายละเอียดทางเทคนิค
รุ่น DOM: | วัตถุเอกสารหลักระดับ 2 |
---|---|
คืนมูลค่า: | HTMLCollection Object ที่แสดงองค์ประกอบ <option> ทั้งหมดในองค์ประกอบ <datalist> องค์ประกอบในคอลเลกชันจะถูกจัดเรียงตามที่ปรากฏในซอร์สโค้ด |
ตัวอย่างเพิ่มเติม
ตัวอย่าง
[ ดัชนี ]
รับค่าของตัวเลือกแรก (ดัชนี 0) ในดาต้าลิสต์:
var x = document.getElementById("browsers").options[0].value;
ผลลัพธ์ของxจะเป็น:
Internet Explorer
ตัวอย่าง
รายการ ( ดัชนี )
รับค่าของตัวเลือกแรก (ดัชนี 0) ในดาต้าลิสต์:
var x = document.getElementById("browsers").options.item(0).value;
ผลลัพธ์ของxจะเป็น:
Internet Explorer
ตัวอย่าง
ชื่อรายการ ( name_or_id )
รับค่าของตัวเลือกด้วย id="google" ในรายการข้อมูล:
var x = document.getElementById("browsers").options.namedItem("google").value;
ผลลัพธ์ของxจะเป็น:
Chrome
ตัวอย่าง
วนรอบตัวเลือกทั้งหมดใน datalist และส่งออกค่าตัวเลือก:
var x = document.getElementById("mySelect");
var txt = "";
var i;
for (i = 0; i < x.options.length; i++) {
txt = txt + x.options[i].value + "<br>";
}
ผลลัพธ์ของtxtจะเป็น:
Internet Explorer
Firefox
Chrome
Opera
Safari
❮ วัตถุดาต้าลิสต์