/** @author tancheng 基于jquery的地区级联组件*/
/**
oid 当前对象的id
fillId 需要填充对象的Id
clearIds 要清空的数据项
fn 回调函数
使用方式
$(function (){
//1.初始化
jQuery("#vProvCode").change(function (){
AreaPlugin.areaChange('vProvCode',['vCityCode','vContyCode'],'vCityCode');
});
jQuery("#vCityCode").change(function (){
AreaPlugin.areaChange('vCityCode',['vContyCode'],'vContyCode');
});
}
建议回显使用数据库查询list循环方式
*/
var AreaPlugin={};
(function ($$){
this.initCode="root";
this.areaChange=function(oId,clearIds,fillId,fn){
//1.通过obj获得当前选中项
var pcode;
if(oId){
obj=jQuery("#"+oId);
pcode =obj.val();
}
if(!pcode)pcode=this.initCode;
if(pcode){
var url=getRootPath()+"baseinfo/destCity_searchArea.action?time="+new Date();
$$.getJSON(url,{"vDestCityCode":pcode},function (data){
if(data){
clearSelect(clearIds);
fillSelect(jQuery("#"+fillId),data);
}
if(typeof fn=='function')
fn.call();
});
}
}
//清空下拉框
this.clearSelect=function(clearIds){
//清空的时候如果第一项value为空,则保留
if(typeof clearIds=="string")
clearIds=[clearIds];
if($$.isArray(clearIds)){
for(var i=0;i<clearIds.length;i++){
//获得第一项的值
var obj=$$("#"+clearIds[i]);
var fistOption=obj.find("option:first");
obj.empty();
if(!fistOption.val())obj.append(fistOption);
}
}
}
//填充option
this.fillSelect=function (obj,ary){
if($$.isArray(ary)){
for(var i=0;i<ary.length;i++){
obj.append("<option value='"+ary[i].V_DEST_CITY_CODE+"'>"+ary[i].V_DEST_CITY_NAME+"</option>");
}
}
}
function getRootPath() {
var pathName = document.location.pathname;
var index = pathName.substr(1).indexOf("/");
var webName = pathName.substr(0,index+1);
return window.location.protocol + '//' + window.location.host + '/'+ webName + '/';
}
AreaPlugin=this;
})(jQuery)