js 输入框增加删除操作

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<script type="text/javascript">

	window.onload = function (){

		var aInput = document.getElementsByTagName("input");

		for(var i = 0; i < aInput.length; i++){
			aInput[i].onclick = function(){
				if(this.checked){
					if(aInput[0].value == ''){
						aInput[0].value += this.value;
					}else{
						aInput[0].value += ',' + this.value;
					}
				}
				else{
					var arr = aInput[0].value.split(",");
					for(var i = 0; i < arr.length; i++){
						if(arr[i] == this.value){
							arr.splice(i,1);
						}
					}

					aInput[0].value = arr;
				}
			}
		}
	}
	</script>
</head>
<body>
	<input type="text"><br>
	<input type="checkbox" value="html">html<br>
	<input type="checkbox" value="js">js<br>
	<input type="checkbox" value="css">css<br>
	<input type="checkbox" value="jquery">jquery<br>
</body>
</html>

  

posted @ 2015-04-19 18:50  mayufo  阅读(980)  评论(0编辑  收藏  举报