HTML 学习笔记(六).add(new Option(earray[i],i),null)、**.options[**.selectedIndex].text、document.bgColor

来源:互联网 发布:java的多线程特点 编辑:程序博客网 时间:2024/06/06 03:44
<script>var earray=['本科','专科','高中'];//js数组定义var carray=['green','red','grey','black','white'];function addOption() {var obj = document.myform.education;for(var i in earray) {//for(var i=0;i<earray.length;i++) {obj.add(new Option(earray[i],i),null);}var cobj = document.myform.chgcolor;for(var ii in carray) {cobj.add(new Option(carray[ii],ii),null);}}window.onload=addOption;function chg() {//alert("chg()");var obj = document.getElementById("edu");var sobj = document.form1.subedu;sobj.length = 0;if(obj.value==0) {sobj.add(new Option("本一","1"),null);//null表示有几个就按照顺序添加进去;sobj.add(new Option("本二","2"),null);sobj.add(new Option("本三","3"),null);}if(obj.value==1) {sobj.add(new Option("专一","1"),null);sobj.add(new Option("专二","2"),null);sobj.add(new Option("专三","3"),null);}if(obj.value==2) {sobj.add(new Option("高一","1"));sobj.add(new Option("高二","2"));sobj.add(new Option("高三","3"));}//alert(obj.value);//alert(obj.options[obj.selectedIndex].text);}function chgclr() {var obj = document.getElementById("chgcolor");document.bgColor=obj.options[obj.selectedIndex].text;}</script>
<form id="form1" name="myform" method="post"><div id="container"><select id="edu" name="education" onChange="chg()"><option value="-999">请选择</option></select><select id="sedu" name="subedu"><option value="-999">请选择</option></select><select id="chgcolor" onChange="chgclr()"><option>请选择颜色</option></select></div></form>

 

原创粉丝点击