这周开发中,我在对一个标签SELECT进行清空的过程中,犯了一个很弱的错误,^_^,代码如下所示:
var select = document.getElementById("ctl00$ContentPlaceHolder2$dropColorDetail");
select.selectedindex = -1;
var k = select.options.length;
for(var iTemp = 0; iTemp <= k; iTemp++)
{
select.options.remove(iTemp);
}
for(var j = 0; j < detailForm.value.colorList.length; j++)
{
if (select.options[j] == undefined)
{
select.options.add(document.createElement("OPTION"));
}
select.options[j].text = detailForm.value.colorList[j].Color;
select.options[j].value = detailForm.value.colorList[j].Color;
}
当j=0的时候,第一项被Remove,那么第二项的索引于是由1变成了0,剩下的依次类推。这样导致了当j=1的时候,事实上Remove的是以前的第三项,最后总是剩下一项没有Remove掉。晕死,那天不知道脑筋怎么那么笨,没有想到这一点,于是采用了
if (select.options[j] == undefined)
{
select.options.add(document.createElement("OPTION"));
}
这种方式解决,昏死。
还好,我在第二天突然间想到了,也就是从最后一项开始清空就可以了,哈哈。
浙公网安备 33010602011771号