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!