如何实现三级联动菜单?
首先先建一个全国各省市县数据库(http://download.csdn.net/detail/feilong_12/5141762)这里有现成的数据库直接考过来就可以用而且比较全面的哦。
下面是:三级联动的html文件(里面写ajax)
<html>
<head>
<script>
function getArea(val,table){
var xhr;
if(window.ActiveXObject){
xhr=new ActiveXOject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xhr=new XMLHttpRequest();
}
var url="ajax.php";
xhr.open("post",url,true);
xhr.onreadystatechange=callback;
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.send("val="+val+"&table="+table);
function callback(){
if(xhr.readyState==4){
if(xhr.status==200){
//alert(xhr.responseText);
document.getElementById(table).innerHTML=xhr.responseText;
}
}
}
}
</script>
</head>
<body onload="getArea('','t_province')">
<select id="t_province" onchange="getArea(this.value,'t_city')"></select>省
<select id="t_city" onchange="getArea(this.value,'t_district')"></select>市
<select id="t_district"></select>县
</body>
</html>
ajax.php文件
<?php
mysql_connect('localhost','root','123');
mysql_select_db('china');
mysql_query("set names utf8");
$val=$_POST['val'];
$table=$_POST['table'];
if($table=='t_province'){
$sql="select ProName from $table order by ProSort";
$result=mysql_query($sql);
$rows=array();
while($row=mysql_fetch_row($result)){
$rows[]=$row;
echo "<option>$row[0]</option>";
}
}else if($table=='t_city'){
$sql="select CityName from t_city where ProId=(select ProID from t_province where ProName='$val')";
$result=mysql_query($sql);
$rows=array();
while($row=mysql_fetch_row($result)){
$rows[]=$row;
echo "<option>$row[0]</option>";
}var_dump($rows);
}
else if($table=='t_district'){
$sql="select DisName from t_district where CityId=(select CityID from t_city where CityName='$val')";
$result=mysql_query($sql);
$rows=array();
while($row=mysql_fetch_row($result)){
$rows[]=$row;
echo "<option>$row[0]</option>";
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。