---
title: "jquery json ajax例子2联动"
author: "salute"
date: "2010-10-25T17:32:37Z"
tags: []
categories: ["Web前端"]
slug: "jquery-json-ajax-2"
draft: false
lastmod: "2010-10-25T17:32:57Z"
id: 793482
url: "https://salute.iteye.com/blog/793482"
---

```java

/**************************************************************** 
 * 機　能： 業種大分類を変わったら、業種中分類を変わる処理
 * 引　数： first
 *       second
 * 戻り値：
****************************************************************/
function changeMClass(first,second){
  first = first.replace(".","\\.");
  second = second.replace(".","\\.");

  $("#"+first).CascadingSelect(
	$("#"+second),
	"/syspro/callsearchnew/changeCustGyoMClass.action?t="+Math.random(),
	{datatype:"json",textfield:"name",valuefiled:"id",parameter:"selectedBClass"},
	function(){
	  //alert("success");
	  var value = $("#"+first).val();
	  // 空白を選択する場合、中分類をクリアする
	  if(null == value || '' == value || undefined == value){
	  	$("#"+second).empty();
	  }
	  $("#"+second).AddOption("","",true,0);
	  if($.browser.msie && selected) {
		$('option[value='+selected+']',this).attr('selected',true);
	  }
	}
  );
}


```

<span style="color:red">jsp</span>

```java

	<td align="left">
	  <s:select id="inputVO.selectedBClass" name="inputVO.selectedBClass" list="custGyoBClassList" listKey="id" listValue="name" headerKey="" headerValue="" cssStyle="width:120px;" onchange="changeMClass('inputVO.selectedBClass','inputVO.selectedMClass');"/>
	  <font style="font-size:12px;padding-left:5px;">(大分類)</font>
	  <s:select id="inputVO.selectedMClass" name="inputVO.selectedMClass" list="custGyoMClassList" listKey="id" listValue="name" headerKey="" headerValue="" cssStyle="width:120px;"/>
	  <font style="font-size:12px;padding-left:5px;">(中分類)</font>
	</td>


```

<span style="color:red">
后台</span>


```java

    /**
     * 
     * @description 大分類を変わった時、中分類リスト内容を変わる。<br>
     * @return JSONString
     * @note なし
     */
    public String changeCustGyoMClassList() {
        // 業種：中分類リスト作成
        custGyoMClassList = callSearchNewService.getCustGyoMClassList(Integer.parseInt(selectedBClass));
        try {
            JsonUtil.sendJson(custGyoMClassList);
        } catch (IOException e) {
            if (log.isErrorEnabled()) {
                log.error(e);
            }
        }
        return null;
    }


package jp.co.syspro.common.util;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import org.apache.struts2.ServletActionContext;

public class JsonUtil {
	/**
	 * jsonデータ戻る
	 * 
	 * @param obj
	 *            データ
	 * @throws IOException
	 */
	public static void sendJson(Object obj) throws IOException {
		JSONArray json = JSONArray.fromObject(obj);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(json.toString());
	}

}



```