先上代碼 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www./1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>級聯(lián)特效</title> </head> <script type="text/javascript"> function change(){ var classes=document.getElementById("classes"); var t_class=classes.value; var people=document.getElementById("people"); //此處如果連寫4個people.options.length=0;代碼就正常了 //people.options.length=0; //people.options.length=0; //people.options.length=0; people.options.length=0; alert("options.length值為:"+people.options.length); switch(t_class){ case "三班": people.add(new Option("小王","小王"),null); people.add(new Option("小李","小李"),null); people.add(new Option("小紅","小紅"),null); people.add(new Option("小軍","小軍"),null); people.add(new Option("小王","小王"),null); people.add(new Option("小李","小李"),null); people.add(new Option("小紅","小紅"),null); people.add(new Option("小軍","小軍"),null); break; case "六班": people.add(new Option("老王","老王"),null); people.add(new Option("老李","老李"),null); people.add(new Option("老紅","老紅"),null); people.add(new Option("老軍","老軍"),null); people.add(new Option("老王","老王"),null); people.add(new Option("老李","老李"),null); people.add(new Option("老紅","老紅"),null); people.add(new Option("老軍","老軍"),null); break; } } </script> <body> <form> <select id="classes" onchange="change()"> <option>--選擇班級--</option> <option value="三班">三班</option> <option value="六班">六班</option> </select> <select id="people"> <option>--選擇人物--</option> </select> </form> </body> </html> 問題在于people.options.length=0;這句話 按道理people.options.length=0;是直接清除了下拉選項, 但是alert后發(fā)現(xiàn),people.options.length=0;只是把下來選項除以2了 也就是說先選擇3班,然偶從3班選到6班的時候,會發(fā)現(xiàn)people.options.length=0;這句話只是把3班一半的人去掉了,仍然有另一半在六班 而再寫一個people.options.length=0;就會剩下1/4,寫三個people.options.length=0;,就剩下1/8,寫4個people.options.length=0;代碼就正常了 想知道options.length=0;這句代碼為什么會出現(xiàn)除以2的情況 |
|