Chosen 是一个JavaScript插件,它能让丑陋的、很长的select选择框变的更好看、更方便,不仅如此,它更扩大了,增长了主动筛选的功能。
本文介绍Chosen联动,具体代码如下:
rush:js;">
var addressResolve = function (options) {
//检测用户传进来的参数是否合法
if (!isValid(options))
return this;
//默认参数
var defaluts = {
proId: 'divProv',cityId: 'divCity',areaId: 'divArea'
};
var opts = $j.extend({},defaluts,options);//使用jQuery.extend 覆盖插件默认参数
var addressInfo = this;
this.provInfo = $j("#" + opts.proId);//省份select对象
this.cityInfo = $j("#" + opts.cityId);//城市select对象
this.areaInfo = $j("#" + opts.areaId);//区县select对象
/*省份初始化方法*/
this.provInfoInit = function () {
var proOpts = "";
$j.each(provinceJson,function (index,item) {
proOpts += "";
});
addressInfo.provInfo.append(proOpts);
addressInfo.provInfo.chosen(); //初始化chosen
addressInfo.cityInfo.chosen();//初始化chosen
addressInfo.areaInfo.chosen();//初始化chosen
};
/*城市选择绑定方法*/
this.selectCity = function () {
addressInfo.cityInfo.empty();
addressInfo.cityInfo.append("");
addressInfo.areaInfo.empty();
addressInfo.areaInfo.append("");
if (addressInfo.provInfo.val() == "选择省份") { //选择无效时直接返回
addressInfo.cityInfo.trigger("liszt:updated");
addressInfo.areaInfo.trigger("liszt:updated");
return;
}
var provId = addressInfo.provInfo.val();//获取选择的省份值
var cityOpts = "";
$j.each(cityJson,item) {
if (item.ProID == provId) {
cityOpts += "";
}
});
addressInfo.cityInfo.append(cityOpts);
addressInfo.cityInfo.trigger("liszt:updated");
addressInfo.areaInfo.trigger("liszt:updated");
};
/*区县选择绑定方法*/
this.selectArea = function () {
if (addressInfo.cityInfo.val() == "选择城市") return;
addressInfo.areaInfo.empty();
addressInfo.areaInfo.append("");
var cityId = addressInfo.cityInfo.val();//获取选择的城市值
var areaOpts = "";
$j.each(areaJson,item) {
if (item.CityID == cityId) {
areaOpts += "";
}
});
addressInfo.areaInfo.append(areaOpts);
addressInfo.areaInfo.trigger("liszt:updated");
};
/*对象初始化方法*/
this.init = function () {
addressInfo.provInfo.append("");
addressInfo.cityInfo.append("");
addressInfo.areaInfo.append("");
addressInfo.provInfoInit();
addressInfo.provInfo.bind("change",addressInfo.selectCity);
addressInfo.cityInfo.bind("change",addressInfo.selectArea);
}
//私有方法,检测参数是否合法
function isValid(options) {
return !options || (options && typeof options === "object") ? true : false;
}
}
以上所述是小编给大家介绍的jQuery中Chosen三级联动实例代码。编程之家 jb51.cc 收集整理的教程希望能对你有所帮助,如果觉得编程之家不错,可分享给好友!感谢支持。