1. 程式人生 > >Select用法記錄

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);