ITeye博客

select级联操作的简便方法

uule · 2012-04-18 15:07 · JSP · Web前端 · Markdown

 

 

<script type="text/javascript">
		window.onload=function(){
			translate();
			var continentsSelect = document.getElementById("continents");
			var countrySelect = document.getElementById("country");
			var citySelect = document.getElementById("city");
			continentsSelect.value="AS";
			doContinentsChang(continentsSelect);
			countrySelect.value="HK";
			doCountryChang(countrySelect);
			citySelect.value="HKG";
			
		}
		function doContinentsChang(obj){
			var Code = obj.value;
			var countrySelect = document.getElementById("country");
			
			var num = countrySelect.options.length;
			for(i = num -1 ; i >=0; i--){
				countrySelect.remove(i);
			}
			
			<c:forEach items="${COUNTRIESLIST}" var="country">
				if('${country.continents.code}' == Code ){
					var objOption = new Option("${country.countryName}"+" - "+"${country.countryNameTc}",'${country.countryCode}');
					countrySelect.options[countrySelect.options.length] = objOption;
				}
			</c:forEach>
		
		}
		function doCountryChang(obj){			
			var countryCode = obj.value;
			var citySelect = document.getElementById("city");
			var continentsSelect = document.getElementById("continents");
			
			<c:forEach items="${COUNTRIESLIST}" var="country">
				if('${country.countryCode}' == countryCode ){
					continentsSelect.value='${country.continents.code}'
				}
			</c:forEach>
			
			var num =citySelect.options.length;
			for(i = num-1; i >= 0; i-- ){
				citySelect.remove(i);
			}
			<c:forEach var='city' items='${CITIESLIST}'>
				if('${city.countries.countryCode}' == countryCode){
					var objOption = new Option("${city.cityName}"+" - "+"${city.cityNameTc}","${city.cityCode}" + " - " + "${city.cityName}");
					citySelect.options[citySelect.options.length] = objOption;
				}
			</c:forEach>
			
		}

		function doCityChang(obj){
			var cityCode = obj.value;
		}
		
		function doConfirm(){
			var citySelect = document.getElementById("city");
			if(citySelect.value==""){
				alert("@TW请选择城市@@CN请选择城市@@ENPlease select city@!");
				return false;
			}
			window.returnValue=citySelect.value;
			window.close();
		}
	</script>

 

<body style="text-align: center;">
	<div align="left" style="width: 380px; padding-left: 10px;">
		<span T:values="@TW地區@@CN地区@@ENContinent@:"></span><br>
		<select id="continents" onchange="doContinentsChang(this)" style="width: 300px;">
			<c:forEach items="${CONTINENTSLIST}" var="continents">
				<option value="${continents.code}">
					${continents.continentsNameEng}(${continents.continentsNameBig5})
				</option>
			</c:forEach>
		</select>
		<br>
		<br>
		<span T:values="@TW國家@@CN国家@@ENCountryCode@:"></span><br>
		<select id="country" onchange="doCountryChang(this);" style="width: 350px;">
			<c:forEach items="${COUNTRIESLIST}" var="country">
				<option value="${country.countryCode}">
					${country.countryName}(${country.countryNameTc})
				</option>
			</c:forEach>
		</select>
		<br>
		<br>
		<span T:values="@TW城市@@CN城市@@ENCity@:"></span><br>
		<select id="city" onchange="doCityChang(this)" style="width: 300px;">
		</select>
		<br>
		<br>
		<br>
		<input type="button" onclick="return doConfirm();" T:values="@TW確認@@CN确认@@ENConfirm@">
		</div>
	</body>

 

主要使用new option(text,value)

将当前值与forEach值遍历构造option.

 

2、改变城市时获取country的简便方法:

 

<table style="width:100%;" border="0">
	<tr>
		<td>
			<span T:values="@TW城市:@@CN城市:@@ENCity:@" ></span>
		</td>
		<td>
			<select name="city" id="city" onChange="doCityChange();" style="width:250px;">
				<option value="-1"></option>
				<c:forEach items="${CITYLISTBYCODE}" var="item" varStatus="i">
					<option value="${item.cityCode},${item.cityName},${item.countries.countryName}">${item.cityName} - ${item.cityNameTw}</option>
				</c:forEach>
			</select>
		</td>
		
		<td>
			<input type="text" readonly="true" name="countryName" id="countryName" style="width:110px;"/>
		</td>
		<td>
			<input type="button" onClick="doSelect();" value="Select" style="width:60px;">
		</td>
	</tr>
</table>

 

 

 

function doCityChange(){
	var city = $("city").value;
	if(city == "-1"){
		$("cityName").value = "";
		$("countryName").value = "";
		return;
	}
	var arr = city.split(",");
	//$("cityName").value = arr[1];
	$("countryName").value = arr[2];
}

遍历City时,将与City关联的Country值放入City的value中,最后分割即可!

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。