相思资源网 Design By www.200059.com

本文实例为大家分享了JavaScript实现省市区三级联动的具体代码,供大家参考,具体内容如下

首先是js

$(document).ready(function(){
 getErpMarketByParentCode(0,'province',province);
 getErpMarketByParentCode(province,'city',city);
 getErpMarketByParentCode(city,'area',area);
 getErpMarketByParentCode(area,'market',market);
 $('#city').append($('<option>').val('').html('-请选择-')).val('');
 $('#area').append($('<option>').val('').html('-请选择-')).val('');
 if($('#market')){
 $('#market').append($('<option>').val('').html('-请选择-')).val('');
 }
 $('#province').change(function(){
 if($(this).val() == ''){ // 请选择
  $('#city').empty();
  $('#area').empty();
  $('#city').append($('<option>').val('').html('-请选择-')).val('');
  $('#area').append($('<option>').val('').html('-请选择-')).val('');
  if($('#market')){
  $('#market').empty();
  $('#market').append($('<option>').val('').html('-请选择-')).val('');
  }
  return;
 }
 getErpMarketByParentCode($(this).val(),'city');
 $('#area').empty();
 $('#area').append($('<option>').val('').html('-请选择-')).val('');
 });
 
 $('#city').change(function(){
 getErpMarketByParentCode($(this).val(),'area');
 if($('#market')){
  $('#market').empty();
  $('#market').append($('<option>').val('').html('-请选择-')).val('');
 }
 });
 
 $('#area').change(function(){
 getErpMarketByParentCode($(this).val(),'market');
 });
});
 
function getErpMarketByParentCode(parentCode, id, value){
 $.ajax({  
   url:'/getErpMarketByParentCode',  
   type:'post',  
   data:{
   code:parentCode
   },  
   dataType : 'json',
   error:function(){  
    alert('error');  
   },  
   success:function(data){  
    var select = $('#' + id);
    if(select){
     select.empty();
    select.append($('<option>').val('').html('-请选择-'));
    $.each(data.erpMarketList,function(i, erpMarket){
     select.append($('<option>').val(erpMarket.code).html(erpMarket.name));
    });
    select.val(value);
    }
   }
 });
}

然后是后台方法

@RequestMapping(value = "/getErpMarketByParentCode")
 public ModelAndView getErpMarketByParentCode(HttpServletRequest request, HttpServletResponse response) throws Exception {
 String code = request.getParameter("code");
 List<ErpMarket> erpMarketList = erpMarketService.getErpMarketListByParentCode(code);
 JSONObject jsonObject = new JSONObject();
 jsonObject.put("erpMarketList",erpMarketList);
 response.getWriter().write(jsonObject.toString());
 return null;
 }

service方法,是从内从中获取省市区这张表

public List<ErpMarket> getErpMarketListByParentCode(String parentCode){
 Hashtable erpMarketTable = LoadOnStartService.erpMarketTable;//从内存中获取这张表
 List<ErpMarket> erpMarketList = new ArrayList<ErpMarket>();
 Iterator its = erpMarketTable.keySet().iterator();
 while(its.hasNext()){
  String key = Util.getString(its.next());
  ErpMarket erpMarket = (ErpMarket)erpMarketTable.get(key);
  if(parentCode.equals(erpMarket.getParentCode())){
  erpMarketList.add(erpMarket);
  }
 }
 return erpMarketList;
 }

省市区的实体类

package com.erp.entity;
 
/**
 * ErpMarket entity.
 * 
 * @author MyEclipse Persistence Tools
 */
 
public class ErpMarket implements java.io.Serializable {
 
 // Fields
 
 private String code;
 private String shortCode;
 private String name;
 private String level;
 private String parentCode;
 
 // Constructors
 
 /** default constructor */
 public ErpMarket() {
 }
 
 /** full constructor */
 public ErpMarket(String shortCode, String name, String level, String parentCode) {
 this.shortCode = shortCode;
 this.name = name;
 this.level = level;
 this.parentCode = parentCode;
 }
 
 // Property accessors
 
 public String getCode() {
 return this.code;
 }
 
 public void setCode(String code) {
 this.code = code;
 }
 
 public String getShortCode() {
 return this.shortCode;
 }
 
 public void setShortCode(String shortCode) {
 this.shortCode = shortCode;
 }
 
 public String getName() {
 return this.name;
 }
 
 public void setName(String name) {
 this.name = name;
 }
 
 public String getLevel() {
 return this.level;
 }
 
 public void setLevel(String level) {
 this.level = level;
 }
 
 public String getParentCode() {
 return this.parentCode;
 }
 
 public void setParentCode(String parentCode) {
 this.parentCode = parentCode;
 }
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
js省市区三级联动,js省市区联动,js三级联动

相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com

评论“JavaScript实现省市区三级联动”

暂无JavaScript实现省市区三级联动的评论...