jquery实现省市二级联动菜单
时间:2021-07-01 10:21:17
帮助过:4人阅读
pro_city.php 初始页面:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>省市二级联动</title>
</head>
<body>
<script type="text/javascript" src="jquery/jquery-1.4.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#province").change(function(){
$("#city").load("show.php?id="+$("#province").val());
});
});
</script>
<span>省市二级联动</span><br/>
<select name="province" id="province">
<option value="-1">请选择省份</option>
<?php
$conn = mysql_connect("localhost","root","root"); //连接数据库
mysql_query("set names utf8");
mysql_select_db("bbs");
$rs = mysql_query("select id,cname,parentid from areas where parentid='0'");
while($row = mysql_fetch_array($rs))
{
?>
<option value="<?php echo $row['id']?>"><?php echo $row['cname']?></option>
<?php
}
?>
</select>
<span id="city">
<select name="city1" id="city1">
<option value='-2'>请选择城市</option>
</select>
</span>
</body>
</html>
show.php
<?php
header("content-type:text/html;charset=utf-8");
$province_id = $_GET['id'];
if($province_id=='10000000' || $province_id=='11000000' || $province_id=='18000000' || $province_id=='31000000')
{
//直辖市
$sql = "select id,cname from areas where id='{$province_id}'";
}
else
{
$sql = "select id,cname from areas where parentid={$province_id}";
}
$conn = mysql_connect("localhost","root","root"); //连接数据库
mysql_query("set names utf8");
mysql_select_db("bbs");
$rs = mysql_query($sql);
$show = "<select name='city1' id='city1'>";
while($row = mysql_fetch_array($rs))
{
$show .= "<option value=".$row['id'].">".$row['cname']."</option>";
}
$show .= "</select>";
if($province_id=='-1') //选中”请选择省份“时,城市列表显示"请选择城市"
{
$show="<select><option>请选择城市</option></select>";
}
echo $show; //返回字符串显示到页面
?>