`
nakupanda
  • 浏览: 410784 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

使用javascript操作多选select框时遇到的问题

阅读更多
假设有一个多选框:
<select id="select1" multiple="multiple">
     <option>HELLO1</option>
     <option>HELLO2</option>
     <option>HELLO3</option>
     ......
     ......
     <option>HELLO1000</option>
</select>


现在我有2个javascript函数,用于移除所有选中的选项,我觉得他们的效果应该一样的:
	//顺序遍历
	function removeSelectedASC()
	{
		obj = document.getElementById('select1');
		for(var i=0;i<obj.length;i++)
		{
			if(obj.options[i].selected)
				obj.remove(i);
		}
	}

	//倒序遍历	
	function removeSelectedDESC()
	{
		obj = document.getElementById('select1');
		for(var i=obj.length-1;i>=0;i--)
		{
			if(obj.options[i].selected)
				obj.remove(i);
		}
	}



但结果是,顺序遍历函数只能移除一半被选中的选项,而倒序遍历函数则使用正常,为什么?
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics