Datalist- Optionen Sammlung
Beispiel
Finden Sie heraus, wie viele Optionen es in einem bestimmten <datalist>-Element gibt:
var x = document.getElementById("browsers").options.length;
Das Ergebnis von x ist:
5
Weitere „Probieren Sie es selbst“-Beispiele weiter unten.
Definition und Verwendung
Die Optionssammlung gibt eine Sammlung aller Optionen in einem <datalist>-Element zurück.
Hinweis: Die Elemente in der Sammlung werden so sortiert, wie sie im Quellcode erscheinen.
Browser-Unterstützung
Collection | |||||
---|---|---|---|---|---|
options | Yes | 10.0 | Yes | Yes | Yes |
Syntax
datalistObject.options
Eigenschaften
Property | Description |
---|---|
length | Returns the number of <option> elements in the collection. Note: This property is read-only |
Methoden
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 |
Technische Details
DOM-Version: | Dokumentobjekt der Kernebene 2 |
---|---|
Rückgabewert: | Ein HTMLCollection-Objekt, das alle <option>-Elemente im <datalist>-Element darstellt. Die Elemente in der Sammlung werden so sortiert, wie sie im Quellcode erscheinen |
Mehr Beispiele
Beispiel
[ Inhaltsverzeichnis ]
Holen Sie sich den Wert der ersten Option (Index 0) in einer Datenliste:
var x = document.getElementById("browsers").options[0].value;
Das Ergebnis von x ist:
Internet Explorer
Beispiel
Artikel ( Index )
Holen Sie sich den Wert der ersten Option (Index 0) in einer Datenliste:
var x = document.getElementById("browsers").options.item(0).value;
Das Ergebnis von x ist:
Internet Explorer
Beispiel
benanntesElement( name_or_id )
Holen Sie sich den Wert der Option mit id="google" in einer Datenliste:
var x = document.getElementById("browsers").options.namedItem("google").value;
Das Ergebnis von x ist:
Chrome
Beispiel
Durchlaufen Sie alle Optionen in einer Datenliste und geben Sie die Optionswerte aus:
var x = document.getElementById("mySelect");
var txt = "";
var i;
for (i = 0; i < x.options.length; i++) {
txt = txt + x.options[i].value + "<br>";
}
Das Ergebnis von txt wird sein:
Internet Explorer
Firefox
Chrome
Opera
Safari
❮ Datalist-Objekt