/*
省市县 三级联动, 后台提取json数据
<script src="res/PCASelect.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
var defaults = {
url: 'PCAget.ashx?code=', //后台json处理文件
sid: 'PP1,CC1,AA1', //select的id
dv: '350000,,', //初始值
ft: '...请选择...,,' //生成selec的第一项 文本
};
$("kk").PCA(defaults);
});
</script>
后台文件:PCAget.ashx
<%@ WebHandler Language="C#" Class="PCAget" %>
using System;
using System.Web;
using System.Text;
using System.Data;
using LC.Data.Helper;
public class PCAget : IHttpHandler {
public void ProcessRequest (HttpContext context) {
// 数组 [{"id":"275","name":"A1"},{"id":"319","name":"A2"},{"id":"322","name":"A3"}]
string code = context.Request["code"];
string strSQL = "select * from s_PCA where parent='" + code + "' order by Sort ";
DataTable dt = appData.Instance().ExecuteDataset(strSQL).Tables[0];
StringBuilder strClass = new StringBuilder();
if (dt != null)
{
strClass.Append("[");
for (int i = 0; i < dt.Rows.Count; i++)
{
strClass.Append("{");
strClass.Append("\"id\":\"" + dt.Rows[i]["code"].ToString() + "\",");
strClass.Append("\"name\":\"" + dt.Rows[i]["name"].ToString() + "\"");
if (i != dt.Rows.Count - 1)
{
strClass.Append("},");
}
}
}
strClass.Append("}");
strClass.Append("]");
context.Response.ContentType = "application/json";
context.Response.ContentEncoding = Encoding.UTF8;
context.Response.Write(strClass.ToString());
context.Response.End();
}
public bool IsReusable {
get {
return false;
}
}
}
*/
(function($) {
var defaults = {
url: 'PCAget.ashx?code=', //后台json处理文件
sid: 'PP,CC,AA', //select的id
dv: ',,', //初始值
ft: '...请选择...,,', //生成selec的第一项 文本
fv: ',,', //生成selec的第一项 值
pid: 0 //最顶级的父ID ,用于后台处理文件
};
$.fn.PCA = function(options) {
//配置参数
defaults = $.extend({}, defaults, options);
var sid = defaults.sid.split(',');
defaults.len = sid.length;
GetA("", defaults.pid); //初始化
//设置change事件
for (var i = 0; i < sid.length; i++) {
$("#" + sid[i]).change(function() {
GetA($(this).attr("id"), $(this).attr("value"))
});
}
};
function GetA(str_id, pid) {
var arrSid = defaults.sid.split(',');
var arrDv = defaults.dv.split(',');
var arrFt = defaults.ft.split(',');
var arrFv = defaults.fv.split(',');
//找出自己
var self;
var kk = 0;
if (str_id != "") {
while (kk <= arrSid.length) {
kk++;
if (arrSid[kk - 1] == str_id)
break;
}
}
self = $("#" + arrSid[kk]);
//清空从自己开始之后所有项
for (var i = kk; i <= arrSid.length; i++) {
$("#" + arrSid[i]).html("");
}
//当前下拉框的第一个选择项
if (arrFt[kk] != "")
self.append("<option value='" + arrFv[kk] + "'>" + arrFt[kk] + "</option>");
//添加option
$.getJSON(defaults.url + pid, function(data) {
for (var i = 0; i < data.length; i++) {
var $opt = $("<option></option>").val(data[i].id).html(data[i].name);
if (arrDv[kk] == data[i].id) $opt.attr("selected", "selected");
self.append($opt);
};
//孩子下拉框
if (arrSid[kk + 1] != undefined)
GetA(arrSid[kk], self.attr("value"));
});
}
})(jQuery);
省市县 三级联动, 后台提取json数据
<script src="res/PCASelect.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
var defaults = {
url: 'PCAget.ashx?code=', //后台json处理文件
sid: 'PP1,CC1,AA1', //select的id
dv: '350000,,', //初始值
ft: '...请选择...,,' //生成selec的第一项 文本
};
$("kk").PCA(defaults);
});
</script>
后台文件:PCAget.ashx
<%@ WebHandler Language="C#" Class="PCAget" %>
using System;
using System.Web;
using System.Text;
using System.Data;
using LC.Data.Helper;
public class PCAget : IHttpHandler {
public void ProcessRequest (HttpContext context) {
// 数组 [{"id":"275","name":"A1"},{"id":"319","name":"A2"},{"id":"322","name":"A3"}]
string code = context.Request["code"];
string strSQL = "select * from s_PCA where parent='" + code + "' order by Sort ";
DataTable dt = appData.Instance().ExecuteDataset(strSQL).Tables[0];
StringBuilder strClass = new StringBuilder();
if (dt != null)
{
strClass.Append("[");
for (int i = 0; i < dt.Rows.Count; i++)
{
strClass.Append("{");
strClass.Append("\"id\":\"" + dt.Rows[i]["code"].ToString() + "\",");
strClass.Append("\"name\":\"" + dt.Rows[i]["name"].ToString() + "\"");
if (i != dt.Rows.Count - 1)
{
strClass.Append("},");
}
}
}
strClass.Append("}");
strClass.Append("]");
context.Response.ContentType = "application/json";
context.Response.ContentEncoding = Encoding.UTF8;
context.Response.Write(strClass.ToString());
context.Response.End();
}
public bool IsReusable {
get {
return false;
}
}
}
*/
(function($) {
var defaults = {
url: 'PCAget.ashx?code=', //后台json处理文件
sid: 'PP,CC,AA', //select的id
dv: ',,', //初始值
ft: '...请选择...,,', //生成selec的第一项 文本
fv: ',,', //生成selec的第一项 值
pid: 0 //最顶级的父ID ,用于后台处理文件
};
$.fn.PCA = function(options) {
//配置参数
defaults = $.extend({}, defaults, options);
var sid = defaults.sid.split(',');
defaults.len = sid.length;
GetA("", defaults.pid); //初始化
//设置change事件
for (var i = 0; i < sid.length; i++) {
$("#" + sid[i]).change(function() {
GetA($(this).attr("id"), $(this).attr("value"))
});
}
};
function GetA(str_id, pid) {
var arrSid = defaults.sid.split(',');
var arrDv = defaults.dv.split(',');
var arrFt = defaults.ft.split(',');
var arrFv = defaults.fv.split(',');
//找出自己
var self;
var kk = 0;
if (str_id != "") {
while (kk <= arrSid.length) {
kk++;
if (arrSid[kk - 1] == str_id)
break;
}
}
self = $("#" + arrSid[kk]);
//清空从自己开始之后所有项
for (var i = kk; i <= arrSid.length; i++) {
$("#" + arrSid[i]).html("");
}
//当前下拉框的第一个选择项
if (arrFt[kk] != "")
self.append("<option value='" + arrFv[kk] + "'>" + arrFt[kk] + "</option>");
//添加option
$.getJSON(defaults.url + pid, function(data) {
for (var i = 0; i < data.length; i++) {
var $opt = $("<option></option>").val(data[i].id).html(data[i].name);
if (arrDv[kk] == data[i].id) $opt.attr("selected", "selected");
self.append($opt);
};
//孩子下拉框
if (arrSid[kk + 1] != undefined)
GetA(arrSid[kk], self.attr("value"));
});
}
})(jQuery);