省份城市菜单联动,ajax3级菜单联动

首先看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&cityselect[@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&cityselect[@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&cityselect[@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&cityselect[@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 = ”

Posted in 未分类

发表评论