-
-12 11
-
昨天自己做了个jquery+php的省市区的三级连选,贴出来让php爱好者和爱好php学习的朋友们分享分享,由于时间匆忙,上面还有很多bug,比如默认时候没有市区,但是思路还是比较清晰,朋友们可以自己发挥主动性,提出你的意见和建议!
XML/HTML代码- <form id="form1" name="form1" method="post" action="">
- <label>省
- <select name="did" id="did">
- <option value="1">湖北</option>
- <option value="2">四川</option>
- </select>
- </label>
- <label>市
- <select name="12x" id="12x">
- <option>选择市 </option>
- </select>
- </label>
- <label>区
- <select name="12x1" id="12x1">
- <option>请选择区 </option>
- </select>
- </label>
- </form>
JavaScript代码- <script type="text/javascript" src="jquery.js"></script>
- <script type="text/javascript">
- $(document).ready(function(){
- $("#did").change(function(){
- var obj = $(this).val();
- //alert(obj);
- $.ajax({
- url:'post.php',
- type:'get',
- data: "pid="+obj,
- dataType:'json',
- success: function(rs){
- // alert(rs);
- var x = '';
- var t='';
- $("#12x").empty();
- //$("<option></option>").val("请选择城市").text("请选择城市").appendTo($("#12x"));
- for(var i=0; i<rs.length; i++){
- $("<option></option>").val(rs[i]['cid']).text(rs[i]['name']).appendTo($("#12x"));
- }
- }
- });
- });
- $("#12x").change(function(){
- var obj = $(this).val();
- //alert(obj);
- $.ajax({
- url:'post.php',
- type:'get',
- data: "cid="+obj,
- dataType:'json',
- success: function(rs){
- //alert(rs);
- var x = '';
- var t='';
- $("#12x1").empty();
- //$("<option></option>").val("请选择区").text("请选择区").appendTo($("#12x1"));
- for(var i=0; i<rs.length; i++){
- $("<option></option>").val(rs[i]['cid']).text(rs[i]['name']).appendTo($("#12x1"));
- }
- }
- });
- });
- })
- </script>
PHP代码- <?php
- /*这个本该从数据库查询的为了好测试我就随便写了*/
- $hubei=array(
- '0'=>array('pid'=>1,
- 'name'=>'恩施',
- 'cid'=>1
- ),
- '1'=>array('pid'=>1,
- 'name'=>'武汉',
- 'cid'=>2
- )
- );
- $sichuan=array(
- '0'=>array('pid'=>2,
- 'name'=>'成都',
- 'cid'=>1
- ),
- '1'=>array('pid'=>2,
- 'name'=>'宜宾',
- 'cid'=>2
- )
- );
- $enshi=array(
- '0'=>array('cid'=>1,
- 'name'=>'建市'
- ),
- '1'=>array('cid'=>1,
- 'name'=>'巴东'
- )
- );
- $wuhan=array(
- '0'=>array('cid'=>2,
- 'name'=>'武汉1'
- ),
- '1'=>array('cid'=>2,
- 'name'=>'武汉2'
- )
- );
- if(isset($_GET['pid']) && $_GET['pid'] == 1){
- $result = json_encode($hubei);
- }else if(isset($_GET['pid']) && $_GET['pid'] == 2){
- $result = json_encode($sichuan);
- }else if(isset($_GET['cid']) && $_GET['cid'] == 1){
- $result = json_encode($enshi);
- }else if(isset($_GET['cid']) && $_GET['cid'] ==2){
- $result = json_encode($wuhan);
- }
- echo $result;
- ?>
本文来源于php爱好者:php教程 —http://www.phplover.cn/
原文地址:http://www.phplover.cn/post/jquery_php.html
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。
- 评论(0)
发表评论 TrackBack