|
淺談<select > 對象中option 的清空
intLength 為 option的個數 :document.all("lstUserId").options.length;
lstUserId 為 <select id=lstUserId> 對象:
<select id=lstUserId>
<option >1</option>
<option >3</option>
<option >4</option>
</select>
方法一:
for (var i=0; i<intLength-1;i++)
{
document.all("lstUserId").options.remove(i);
}
方法二:
while (document.all("lstUserId").options.length>0)
{
document.all("lstUserId").option.remove(0);
}
方
法一不能清空<select id=lstUserId>對象并且會報錯。因為當
document.all("lstUserId").options[0]被刪除后
document.all("lstUserId").options[intLength-1] 即最后一個option就不復存在了。
所以從最后一個刪起可以的從第一個刪起始有問題的。
而
方法二是能清空的因為document.all("lstUserId").options[0]刪除后
document.all("lstUserId").options[1]變成了
document.all("lstUserId").options[0]。直至刪到最后一個。 |