Select用法記錄

 <select id="test" style="width: 100px;">
		 	<option value="2"></option>
		 	<option value="1">1</option>
		 	<option value="w">2</option>
		 	<option value="e">3</option>
		 	<option value="f">4</option>
 </select>
//功能:實現設置默認選項
//用Hbuilder老報“Uncaught TypeError: Cannot read property 'options' of null”
//不知道是不是什麼我沒引入,但在(MVC)vs2012是可以的
var actionLength = document.getElementById("test");                         
for (var i = 0; i < actionLength.options.length; i++) {
	if (actionLength[i].innerText == "2") {
		actionLength[i].selected = true;        
        break;
	}
}
//注:也可寫成
var actionLength = document.getElementById("test").options;
console.log(actionLength[i].innerText);

猜你喜欢

转载自blog.csdn.net/qq_41128526/article/details/85702607