挺简洁的一个2级联动
2010-08-24 12:03
<script language="JavaScript" type="text/javascript">var n=[
["体验1","广东1","广东2","广东3","广东4","广东5","广西1","福建1","福建2","广东6","福建2"],
["体验2","北京1","北京2","北京3","北京4","山东1","河南1","北京5","山东2"],
["上海1","上海2","上海3","上海4","浙江1","江苏1","上海5","浙江2","安徽1","江苏2","安徽2","浙江3","江苏3"],
["四川1","四川2","重庆1","云南1","贵州1"],
["湖北1","湖南1","湖北2","江西1"],
["辽宁1","吉林1","黑龙江1"]
];
function get_(){
var yi=document.getElementById("yi"); //获取1级菜单
var er=document.getElementById("er"); //获取2级菜单
var nn=n[yi.selectedIndex-1]; //获取2级菜单内容
er.length=1; //默认项不改变
for(var i=0;i<nn.length;i++){ //循环增加新项
er[i+1]=new Option(nn[i],nn[i]);
}
}</script>
<select name="" onchange="get_();" id="yi" style="width:100px;">
<option value="大区">所在大区</option>
<option value="华南">华南(电信)</option>
<option value="华北">华北(网通)</option>
<option value="华东">华东(电信)</option>
<option value="西南">西南(电信)</option>
<option value="华中">华中(电信)</option>
<option value="东北">东北(网通)</option>
</select>
<select id="er" style="width:100px;">
<option value="服务器">所在服务器</option>
</select> ----------------------------------------------------在弄个带php程序的 json
json对中文支持不够好 要用 urlencode()把要传递的字符串编码下
$json_string = json_encode($arr);
echo urldecode($json_string);
页面直接输出.直接看例子吧---------------------------------------------------
<?
$fl="1.inc"; //我用的文本 你随便啦
$f=fopen($fl,"r");
$contents = fread($f, filesize ($fl));
fclose($f); //取出字符串
$arrf=split('|',$contents); //按照大类切开
$arr=array();for($i=0;$i<count($arrf);$i++){
$arr[$i]=split(',',$arrf[$i]); //在切开小类
for($k=0;$k<count($arr[$i]);$k++){
$arr[$i][$k]=urlencode($arr[$i][$k]); //编码
}
}
$json_string = json_encode($arrf);
$str=urldecode($json_string);
?>
<script>
var cityp=<$=$str$>;
</script>1.inc 内容
---------------------------------------------------------
体验1,广东1,广东2,广东3,广东4,广东5,广西1,福建1,福建2,广东6,福建2|
体验2,北京1,北京2,北京3,北京4,山东1,河南1,北京5,山东2|
上海1,上海2,上海3,上海4,浙江1,江苏1,上海5,浙江2,安徽1,江苏2,安徽2,浙江3,江苏3|
四川1,四川2,重庆1,云南1,贵州1|
湖北1,湖南1,湖北2,江西1|
辽宁1,吉林1,黑龙江1