struts2 jquery json ajax 三级联动菜单

转自:http://blog.sina.com.cn/s/blog_4a9f3eea0100dvgm.html

js代码

  

$(document).ready(

function(){

$.ajax({

url:"../adminManage/bindTheater.action",

type:"GET",

dataType:"json",

success:bindTheaterList

});

}

);

functionbindTheaterList(json){

//把返回的json字符串赋值给变量data

vardata=(json.theaterString);

//遍历json对象

for(vartheaterindata){

//varoption=document.createElement("option");

//document.getElementByIdx("bindTheater").appendChild(option);

//option.value=data[theater].theaterId.value;

//option.text=data[theater].name.value;

//临时变量判断循环次数

varflag=0;

varoption=document.createElement("option");

//json对象中的一个元素

for(varkeyindata[theater]){

//创建一个option

document.getElementByIdx("bindTheater").appendChild(option);

if(flag==0){

option.value=data[theater][key];

flag++;

}else{

option.text=data[theater][key];

flag=0;

}

}

}

}

functiongetHall(){

//绑定之前清空第一个以外的option

$("#bindHall").children().eq(0).siblings().remove();

$("#bindShow").children().eq(0).siblings().remove();

vartemp=$("#bindTheater").find("option:selected").val();

$.ajax({

url:"../adminManage/bindHall.action",

type:"get",

dataType:"json",

data:"theaterid="+temp,

success:bindHallList

});

}

//回调函数

functionbindHallList(json){

data=(json.hallString);

for(hallindata){

varoption=document.createElement("option");

for(keyindata[hall]){

document.getElementByIdx("bindHall").appendChild(option);

option.text=data[hall][key];

}

}

}

functiongetShow(){

//绑定之前清空第一个以外的option

$("#bindShow").children().eq(0).siblings().remove();

vartemp=$("#bindHall").find("option:selected").val();

$.ajax({

url:"../adminManage/bindShow.action",

type:"get",

dataType:"json",

data:"hallid="+temp,

success:bindShowList

});

}

//回调函数

functionbindShowList(json){

data=(json.showString);

for(showindata){

varoption=document.createElement("option");

for(keyindata[show]){

document.getElementByIdx("bindShow").appendChild(option);

option.text=data[show][key];

}

}

     }

页面代码

<body>

<center>

<div>

<SPAN>

影院:

<selectid="bindTheater"onChange="getHall()"style="width:100px;"></select>

</SPAN>

<SPAN>

大厅:

<selectid="bindHall"onChange="getShow()"style="width:100px;">

<optionvalue="-1">--</option>

</select>

</SPAN>

<span>

场次:

<selectid="bindShow"style="width:100px;">

<optionvalue="-1">--</option>

</select>

</span>

</div>

</center>

<s:submitid="submit"value="生成票"onClick="genarateTickets()"></s:submit>

<div>

</div>

  </body>

配置文件

<package name="adminManage" extends="struts-default,json-default" namespace="/adminManage">

<action name="bindTheater" class="AdminManagementAction" method="bindTheater">

<resulttype="json">

</result>

</action>

<actionname="bindHall"class="AdminManagementAction"method="bindHall">

<resulttype="json"></result>

</action>

<actionname="bindShow"class="AdminManagementAction"method="bindShow">

<resulttype="json"></result>

  </action>

</package>

action代码

public String bindTheater() throws FileNotFoundException, IOException{ 

List<Theater>list=adminService.findAllTheter();

StringBuildersb=newStringBuilder();

intsize=list.size();

sb.append("[");

for(Theatertheater:list){

size--;

sb.append("{theaterId:\"");

sb.append(theater.getTheaterId());

sb.append("\",theaterName:\"");

sb.append(theater.getName());

sb.append("\"}");

if(size>0){

sb.append(",");

}

}

sb.append("]");

this.theaterString=sb.toString();

returnSUCCESS;

}

publicStringbindHall(){

List<Hall>list=this.adminService.findHallBytheaterId(Integer.parseInt(this.theaterId));

StringBuildersb=newStringBuilder();

intsize=list.size();

sb.append("[");

for(Hallhall:list){

size--;

sb.append("{hallId:\"");

sb.append(hall.getHid());

sb.append("\",hallId:\"");

sb.append(hall.getHid());

sb.append("\"}");

if(size>0){

sb.append(",");

}

}

sb.append("]");

this.hallString=sb.toString();

returnSUCCESS;

}

publicStringbindShow(){

List<Show>list=this.adminService.findShowByHallId(Integer.parseInt(this.hallId));

StringBuildersb=newStringBuilder();

intsize=list.size();

sb.append("[");

for(Showshow:list){

size--;

sb.append("{hallId:\"");

sb.append(show.getSid());

sb.append("\",hallId:\"");

sb.append(show.getStartTime());

sb.append("\"}");

if(size>0){

sb.append(",");

}

}

sb.append("]");

this.showString=sb.toString();

returnSUCCESS;

 }

这里要暴露出几个私有的字段给json序列化(省略getter和setter)

private String theaterString;

privateStringhallString;

privateStringshowString;

privateStringtheaterId;

privateStringhallId;

privateStringshowId;

相关推荐