Wybierz metodę add()
Przykład
Dodaj opcję „Kiwi” na końcu listy rozwijanej:
var x = document.getElementById("mySelect");
var option = document.createElement("option");
option.text = "Kiwi";
x.add(option);
Definicja i użycie
Metoda add() służy do dodawania opcji do listy rozwijanej.
Wskazówka: Aby usunąć opcję z listy rozwijanej, użyj metody remove() .
Obsługa przeglądarki
Method | |||||
---|---|---|---|---|---|
add() | Yes | Yes | Yes | Yes | Yes |
Składnia
selectObject.add(option, index)
Wartości parametrów
Parameter | Description |
---|---|
option | Required. Specifies the option to add. Must be an option or optgroup element |
index | Optional. An integer that specifies the index position for where the new option element should be inserted. Index starts at 0. If no index is specified, the new option will be inserted at the end of the list |
Szczegóły techniczne
Wartość zwrotu: | Brak wartości zwrotu |
---|
Więcej przykładów
Przykład
Dodaj opcję „Kiwi” na początku listy rozwijanej:
var x = document.getElementById("mySelect");
var option = document.createElement("option");
option.text = "Kiwi";
x.add(option, x[0]);
Przykład
Dodaj opcję „Kiwi” w pozycji indeksu „2” listy rozwijanej:
var x = document.getElementById("mySelect");
var option = document.createElement("option");
option.text = "Kiwi";
x.add(option, x[2]);
Przykład
Dodaj opcję przed wybraną opcją z listy rozwijanej:
var x = document.getElementById("mySelect");
if (x.selectedIndex >= 0) {
var option = document.createElement("option");
option.text = "Kiwi";
var sel = x.options[x.selectedIndex];
x.add(option, sel);
}
❮ Wybierz obiekt