我在网上找了很多json与联级下拉框(父子下拉框)结合的例子,有的用jquery去实现,但我觉得不用那么复习,其实可以简单点的!
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>最简单的json联级下拉框</title>
<script>
var json=[{"CS_Id":"1","CS_FileType":".java","CS_Path":"/java/","CS_Name":"snatch"},{"CS_Id":"4","CS_FileType":".java","CS_Path":"/java/","CS_Name":"search"}];
var json2=[{"CC_Name":"tw_com_yahoo_buy","CS_Id":"1","CC_Id":"1"},{"CC_Name":"com_baidu","CS_Id":"4","CC_Id":"2"}];
function selectReload(){
select1.options.length=0;
select2.options.length=0;
for(var i=0; i<json.length; i++)
{
//alert(json[i].CS_Id);
select1.add(new Option(json[i].CS_Name,json[i].CS_Id));
}
}
function chsel(){
select2.options.length=0;
for(var i=0; i<json2.length; i++){
if(json2[i].CS_Id==select1.value){
select2.add(new Option(json2[i].CC_Name,json2[i].CC_Id));
}
}
}
</script>
</head>
<body>
父:<select name="select1" Id="select1" οnchange="chsel();">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
子:<select name="select2" Id="select2">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</body>
<script>
selectReload();
chsel();
</script>
</html>