js控制select多选

<select multiple="multiple">
	<option value="">AAAA</option>
	<option value="">BBBB</option>
	<option value="">CCCC</option>
	<option value="">DDDD</option>
</select>
<input type="button" value="选中添加到右侧" onclick="toRight()">
<input type="button" value="全部添加到右侧" onclick="toRightAll()">
</div>


<div>
<select multiple="multiple">
	<option value="">EEE</option>
	<option value="">FFF</option>
	<option value="">GGG</option>
	<option value="">HHH</option>
</select>
<input type="button" value="选中添加到左侧" onclick="toLeft()">
<input type="button" value="全部添加到左侧" onclick="toLeftAll()">
</div>


function toLeft() {
		var left=document.getElementsByTagName("select")[0];
		var right=document.getElementsByTagName("select")[1];

		var sel=right.getElementsByTagName("option");
		for (var i = 0; i < sel.length; i++) {
			if (sel[i].selected==true) {
				left.appendChild(sel[i]);
				i--;
			}
		}
	}

	function toRight() {
		var left=document.getElementsByTagName("select")[0];
		var right=document.getElementsByTagName("select")[1];

		var sel=left.getElementsByTagName("option");
		for (var i = 0; i < sel.length; i++) {
			if (sel[i].selected==true) {
				right.appendChild(sel[i]);
				i--;//!!!!!
			}
		}
	}

	function toLeftAll () {

		var left=document.getElementsByTagName("select")[0];
		var right=document.getElementsByTagName("select")[1];
		var sel=right.getElementsByTagName("option");
		for (var i = 0; i < sel.length; i++) {
			
				left.appendChild(sel[i]);
				i--;//!!!!
			
		}
	}
	function toRightAll () {
		var left=document.getElementsByTagName("select")[0];
		var right=document.getElementsByTagName("select")[1];
		var sel=left.getElementsByTagName("option");
		for (var i = 0; i < sel.length; i++) {
			
				right.appendChild(sel[i]);
				i--;
			
		}
		
	}


posted @ 2015-09-15 13:09  Fighter,  阅读(1740)  评论(0编辑  收藏  举报