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

黄舟
黄舟Original
2017-07-28 09:32:402241browse

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;  
    }  
}
/** 
 * 设置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;  
        }  
    }  
};

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!

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