JS獲取頁面select元素

	<select id="edu" name="edu">
		<option value="高級">博士</option>
		<option value="中級">碩士</option>
		<option value="初級" selected="selected">本科</option>
		<option value="低級">大專</option>
	</select>



<script>
	var a=document.getElementById('edu');
	alert("選擇欄位的值:"+a.value);
	//顯示selelct內的option欄位頁面
	alert("a.innerHTML:"+a.innerHTML);
	/*遍歷option 方法1*/
	/*var b=a.getElementsByTagName("option");
	for(var i=0;i<b.length;i++){
		var bb=b[i];
		var bbb=bb.firstChild;
		alert("option子元素的nodeValue"+bbb.nodeValue);
	}*/
	/*遍歷option 方法2*/
	/*var c=a.options;
	for(var i=0;i<c.length;i++){
		//var cc=c[i].value;//獲取value
		var cc=c[i].text;//獲取option之間的text
		alert("="+cc);
	}*/
	var dd=a.options[a.selectedIndex].text;
	alert("顯示欄位選擇的文本:"+dd); 
	
</script>




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章