Home  >  Article  >  Web Front-end  >  Improved version: Add, modify, and delete option elements in select_form effects

Improved version: Add, modify, and delete option elements in select_form effects

WBOY
WBOYOriginal
2016-05-16 19:25:001047browse

After returning from home today after taking annual leave, I saw the "Monitoring and Management Demonstration Code" posted by Xiao Lin on QQ. The core principle is the add() method of the select element:

Copy code The code is as follows:

function watch_ini(){ // Initial
for(var i=0; i var word = document.createElement("OPTION");
word.text = arguments[i];
watch.keywords.add(word); // watch. is form name
}
}
function watch_add(f){ // Add
var word = document.createElement("OPTION");
word.text = f.word.value;
f. keywords.add(word);
}

But the above add() method is only valid under IE. In order to be compatible with FF and Opera, the above code has been improved. The modified code is as follows :
Copy code The code is as follows:

function watch_ini(){ // Initial
for(var i=0; i var oOption=new Option(arguments[i],arguments[i]);
document.getElementById("MySelect").options[ i]=oOption;
}
}
function watch_add(f){ // Add
var oOption=new Option(f.word.value,f.word.value);
f.keywords.options[f.keywords.length]=oOption;
}

The complete code of the entire instance is as follows:

[Ctrl A select all Note:
If you need to introduce external Js, you need to refresh to execute ]
Statement:
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