Home > Web Front-end > JS Tutorial > body text

JavaScript related operation instructions on select_form effects

WBOY
Release: 2016-05-16 18:36:31
Original
1032 people have browsed it
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
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!