jquery实现省市二级联动菜单
程序员文章站
2022-03-09 17:14:50
...
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; //返回字符串显示到页面
?>
<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; //返回字符串显示到页面
?>
推荐阅读
-
使用 Jquery+Ajax+xml制作中国地区选择三级联动菜单
-
ThinkPHP使用心得分享-ThinkPHP + Ajax 实现2级联动下拉菜单_PHP
-
js实现HTML中Select二级联动实例分享
-
JavaScript结合HTML DOM实现联动菜单
-
ThinkPHP使用心得分享-ThinkPHP + Ajax 实现2级联动下拉菜单_PHP教程
-
Javascript实现简单二级下拉菜单实例_javascript技巧
-
jsp jquery实现级联菜单,jquery对select元素的简单操作
-
jQuery实现可收缩展开的级联菜单实例代码_javascript技巧
-
基于jQuery实现的菜单切换效果_jquery
-
jQuery实现的经典竖向伸缩菜单效果代码