Home > Web Front-end > JS Tutorial > Analysis of how JavaScript dynamically sets the selected Option in Select

Analysis of how JavaScript dynamically sets the selected Option in Select

黄舟
Release: 2017-07-28 09:32:40
Original
2263 people have browsed it

How JavaScript dynamically sets the selected instance analysis of Option in Select

var select = document.getElementById("selectYear");  
var nextYear = '2012';  
for(var i=0; i<select.options.length; i++){  
    if(select.options[i].innerHTML == nextYear){  
        select.options[i].selected = true;  
        break;  
    }  
}
Copy after login
/** 
 * 设置select选中 
 * @param selectId select的id值 
 * @param checkValue 选中option的值 
 * @author lqy 
 * @since 2015-08-21 
*/  
function setSelectChecked(selectId, checkValue){  
    var select = document.getElementById(selectId);  
    for(var i=0; i<select.options.length; i++){  
        if(select.options[i].innerHTML == checkValue){  
            select.options[i].selected = true;  
            break;  
        }  
    }  
};
Copy after login

The above is the detailed content of Analysis of how JavaScript dynamically sets the selected Option in Select. For more information, please follow other related articles on the PHP Chinese website!

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