首页  >  文章  >  后端开发  >  省份城市菜单联动,AJAX 3级菜单联动_PHP教程

省份城市菜单联动,AJAX 3级菜单联动_PHP教程

WBOY
WBOY原创
2016-07-13 17:37:58760浏览

首先看JQ的扩展如下:

//city selected

jQuery.fn.cityselect = function(n,s1,s2,s3) {
if ((n!=3)&&(n!=2)) {n=2};
if (s1=="") {s1="province"};
if (s2=="") {s2="city"};
if (s3=="") {s3="area"};

if (n==2){//二级联动
   //alert(s1+$("select[@name="+s1+"] option[@selected]").val());
      $("select[@name="+s1+"]").bind("change",function(event) {
      $.ajax({
        type: "GET",
        url: "/inc/lib/ajax_city.php",
        data: "n=v2&cityid="+$("select[@name="+s1+"] option[@selected]").val(),
        success: function(opt){$("select[@name="+s2+"]").empty().append(opt);}
       });
     });

}

if (n==3){//三级联动

   $("select[@name="+s1+"]").bind("change",function(event) {
      $.ajax({
        type: "GET",
        url: "/inc/lib/ajax_city.php",
        data: "n=v2&cityid="+$("select[@name="+s1+"] option[@selected]").val(),
        success: function(opt){
        $("select[@name="+s2+"]").empty().append(opt);
           $.ajax({
          type: "GET",
          url: "/inc/lib/ajax_city.php",
          data: "n=v3&cityid="+$("select[@name="+s2+"] option[@selected]").val(),
          success: function(opt){
          $("select[@name="+s3+"]").empty().append(opt);
          }
         });
        }
       });
     });
    $("select[@name="+s2+"]").bind("change",function(event) {    
      $.ajax({
      type: "GET",
      url: "/inc/lib/ajax_city.php",
      data: "n=v3&cityid="+$("select[@name="+s2+"] option[@selected]").val(),
      success: function(opt){ $("select[@name="+s3+"]").empty().append(opt);}
     });
  
   });

    }

};

接下来是PHP里的函数如下========

//城市联动脚本
function cityselect($n=2,$s1=province,$s2=city,$s3=area,$v1=320000,$v2=320500,$v3=320501){
   $db = new DB_Sql;       //初始化数据库
$db->connect(DB_Database, DB_Host, DB_User, DB_Password); //数据库连接
$db->DB_Sql("set names gbk");
//联动AJAX脚本
$tmp = " ";
//省份选择
$tmp .= " ";
//城市选择
$tmp .= " ";


if ($n==3){
//地区选择
$tmp .= " ";
}
echo $tmp;
}

调用的时候:

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/486524.htmlTechArticle首先看JQ的扩展如下: //city selected jQuery.fn.cityselect = function(n,s1,s2,s3) { if ((n!=3)(n!=2)) {n=2}; if (s1==) {s1=province}; if (s2==) {s2=city}; if (s3==) {s3=are...
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn