先上代碼
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>級(jí)聯(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個(gè)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>--選擇班級(jí)--</option>
<option value="三班">三班</option>
<option value="六班">六班</option>
</select>
<select id="people">
<option>--選擇人物--</option>
</select>
</form>
</body>
</html>
問(wèn)題在于people.options.length=0;這句話
按道理people.options.length=0;是直接清除了下拉選項(xiàng),
但是alert后發(fā)現(xiàn),people.options.length=0;只是把下來(lái)選項(xiàng)除以2了
也就是說(shuō)先選擇3班,然偶從3班選到6班的時(shí)候,會(huì)發(fā)現(xiàn)people.options.length=0;這句話只是把3班一半的人去掉了,仍然有另一半在六班
而再寫一個(gè)people.options.length=0;就會(huì)剩下1/4,寫三個(gè)people.options.length=0;,就剩下1/8,寫4個(gè)people.options.length=0;代碼就正常了
想知道options.length=0;這句代碼為什么會(huì)出現(xiàn)除以2的情況