//Create an option object, that is, create one or more
/ in the < ;option>My bag
onclick="number() ;">
1. Dynamically create select
function createSelect(){
var mySelect = document.createElement("select"); mySelect.id = "mySelect"; document.body.appendChild(mySelect); }
2 .Add option option
function addOption(){
//Find the object based on id, var obj=document.getElementById('mySelect');
/ /Add an option obj.add(new Option("text","value")); //This is only valid in IE obj.options.add(new Option("text"," value")); //This is compatible with IE and firefox }
3. Delete all options option
function removeAll(){ var obj=document.getElementById('mySelect');
obj.options.length=0;
}
4. Delete an option option
function removeOne(){ var obj=document.getElementById('mySelect');
//index, the serial number of the option to be deleted, take here The serial number of the currently selected option
var index=obj.selectedIndex; obj.options.remove(index); }
5. Get the value of option
var obj=document.getElementById('mySelect');
var index=obj.selectedIndex; //Serial number, take the serial number of the currently selected option
var val = obj.options [index].value;
6. Get the text of option option
var obj=document.getElementById('mySelect');
var index=obj.selectedIndex; //Serial number, take the serial number of the currently selected option
var val = obj.options[index].text;
7. Modify the option option
var obj=document. getElementById('mySelect');
var index=obj.selectedIndex; //Serial number, take the serial number of the currently selected option
var val = obj.options[index]=new Option(" New text","new value");
8. Delete select
function removeSelect(){ var mySelect = document.getElementById("mySelect"); mySelect .parentNode.removeChild(mySelect); }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//ZH-CN""http://www.w3.org/TR/html4/strict.dtd">
The above is the detailed content of HTML select option detailed description. For more information, please follow other related articles on the PHP Chinese website!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn