无限下拉列表

无限增加下拉列表,每个列表的值不重复。

即如果选择列表1中的2,那么第2个列表就不会有这个值了。


代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>

<body>
	
	<div id='div1'>

	</div>
	<input type='button' value='Add' οnclick='addDivSelect();'>

	<script language="JavaScript">
	<!--
		var div1 = document.getElementById('div1');
		var currentSelectIndex = 0;
		var select_option_data = new Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31);
		// 保存当前所有的下拉列表的索引
		var select_array_all = new Array();
		// 保存需要改变的下拉列表的索引
		var chg_select_array = new Array();
		// 保存所有下拉列表选中的值,以便在添加下拉列表时使用。
		var selected_opts = new Array;

		getSelectedOpts = function() {
			var idx = 0;
			for (var i=0;i< select_array_all.length; i++)
			{
				var sel = document.getElementById('select_' + select_array_all[i]);
				if (sel)
				{
					selected_opts[idx] = sel.options[sel.selectedIndex].value;
					idx++;
				}
			}
		}

		checkExist = function(sel_opt_val) {
			for (var i=0;i< selected_opts.length;i++ )
			{
				if (selected_opts[i] == sel_opt_val)
				{
					return true;
				}
			}

			return false;
		}

		addDivSelect = function() {
			currentSelectIndex++;
			div1.innerHTML += "<span id='span_" + currentSelectIndex + "'><select id='select_" + currentSelectIndex + "' οnchange='chgSelect("+currentSelectIndex+")'></select><input type='button' value='-' οnclick='deleteSelect("+currentSelectIndex+")'></span>";

			var curSelect = document.getElementById('select_' + currentSelectIndex);

			getSelectedOpts();

			for (var i=0;i< select_option_data.length;i++ )
			{
				var bol = checkExist(select_option_data[i]);
				if (!bol)
				{
					var opt = new Option(select_option_data[i],select_option_data[i]);
					curSelect.options.add(opt);
				}
			}

			select_array_all[currentSelectIndex-1] = currentSelectIndex;
			
		}

		deleteSelect = function(currentSelectIndex) {
			var del_span = document.getElementById('span_' + currentSelectIndex);
			div1.removeChild(del_span);
			var arr = new Array;
			var arr_index = 0;

			for (var i=0;i< select_array_all.length;i++ )
			{
				if (select_array_all[i] != currentSelectIndex)
				{
					arr[arr_index] = select_array_all[i];
					arr_index++;
				}
			}

			select_array_all = arr;
		}

		chgSelect = function(currentSelectIndex) {
			// 当前下拉列表
			var cur_select = document.getElementById('select_' + currentSelectIndex);
			// 改变其他的下拉列表
			for (var i=0;i<select_array_all.length ;i++ )
			{
				if (select_array_all[i] != currentSelectIndex)
				{
					// 去掉当前下拉列表选中的值
					// 得到要去掉option的下拉列表对象
					var del_sel = document.getElementById('select_' + select_array_all[i]);
					if (del_sel)
					{
						for (var j=0;j<del_sel.options.length ;j++ )
						{
							if (del_sel.options[j].value == cur_select.options[cur_select.selectedIndex].value)
							{
								del_sel.options[j] = null;
							}
						}
					}
				}
			}
		}
	//-->
	</script>
</body>
</html>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值