数据库查询级联插件

/** @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)


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值