1. Insert option
1. DOM method
var oSelectYear = document.getElementById("SelectYear");
var op = document.createElement("option");
op.innerHTML = "2010";
op.value = "2010";
oSelectYear.appendChild(op);
2. new Option method
var oSelectMonth = document.getElementById( "SelectMonth");
oSelectMonth.options.add(new Option(1, 1));
oSelectMonth.options.add(new Option(2, 2));
2. Clear options
var oSelectMonth = document.getElementById("SelectMonth");
oSelectMonth.options.length = 0; //Clear options in Select
3 , Set the default selected option
var oSelectMonth = document.getElementById("SelectMonth");
//oSelectMonth.selectedIndex = 1; //Method 1: The second item is selected by default
//setTimeout (function() { oSelectMonth.selectedIndex = 1; }, 0); //Use setTimeout delay to prevent dom rendering problems
// oSelectMonth.options[1].selected = true; //Method 2
oSelectMonth.options[1].setAttribute("selected", "true"); //Method 3: It is recommended to use setAttribute to set
Statement of this 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