`
free_xiang
  • 浏览: 50887 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

struts2+json+jquery 级联查询

    博客分类:
  • java
阅读更多

js代码


$(document).ready(
function(){
$.ajax({
url:"../adminManage/bindTheater.action",
type:"GET",
dataType:"json",
success:bindTheaterList
});
}
);

function bindTheaterList(json){
// 把返回的json字符串赋值给变量data
var data=(json.theaterString);
//遍历json对象
for(var theater in data){
//var option = document.createElement("option");
//document.getElementByIdx("bindTheater").appendChild(option);
//option.value=data[theater].theaterId.value;
//option.text=data[theater].name.value;
//临时变量判断循环次数
var flag=0;
var option=document.createElement("option");
//json对象中的一个元素
for(var key in data[theater]){
//创建一个option
document.getElementByIdx("bindTheater").appendChild(option);
if(flag==0){

option.value=data[theater][key];
flag++;
}else{
option.text=data[theater][key];
flag=0;
}
}
}
}


function getHall(){
//绑定之前 清空第一个以外的option
$("#bindHall").children().eq(0).siblings().remove();
$("#bindShow").children().eq(0).siblings().remove();
var temp=$("#bindTheater").find("option:selected").val();
$.ajax({
url:"../adminManage/bindHall.action",
type:"get",
dataType:"json",
data:"theaterId="+temp,
success:bindHallList
});
}
//回调函数
function bindHallList(json){
data=(json.hallString);
for(hall in data){
var option = document.createElement("option");
for(key in data[hall]){
document.getElementByIdx("bindHall").appendChild(option);
option.text=data[hall][key];
}
}
}

function getShow(){
//绑定之前 清空第一个以外的option
$("#bindShow").children().eq(0).siblings().remove();
var temp = $("#bindHall").find("option:selected").val();
$.ajax({
url:"../adminManage/bindShow.action",
type:"get",
dataType:"json",
data:"hallId="+temp,
success:bindShowList
});
}
//回调函数
function bindShowList(json){
data=(json.showString);
for(show in data){
var option = document.createElement("option");
for(key in data[show]){
document.getElementByIdx("bindShow").appendChild(option);
option.text=data[show][key];
}
}
}

页面代码

<body>
<center>
<div>
<SPAN>
影院:
<select id="bindTheater" onChange="getHall()" style="width:100px;"></select>
</SPAN>
<SPAN>
大厅:
<select id="bindHall" onChange="getShow()" style="width:100px;">
<option value="-1">--</option>
</select>
</SPAN>
<span>
场次:
<select id="bindShow" style="width:100px;">
<option value="-1">--</option>
</select>
</span>
</div>
</center>
<s:submit id="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">
<result type="json">
</result>
</action>

<action name="bindHall" class="AdminManagementAction" method="bindHall">
<result type="json"></result>
</action>

<action name="bindShow" class="AdminManagementAction" method="bindShow">
<result type="json"></result>
</action>

</package>

action代码

public String bindTheater() throws FileNotFoundException, IOException{
List<Theater> list=adminService.findAllTheter();
StringBuilder sb= new StringBuilder();
int size=list.size();
sb.append("[");
for (Theater theater : 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();
return SUCCESS;
}

public String bindHall(){
List<Hall> list=this.adminService.findHallBytheaterId(Integer.parseInt(this.theaterId));
StringBuilder sb= new StringBuilder();
int size=list.size();
sb.append("[");
for (Hall hall : 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();
return SUCCESS;
}

public String bindShow(){
List<Show> list=this.adminService.findShowByHallId(Integer.parseInt(this.hallId));
StringBuilder sb= new StringBuilder();
int size=list.size();
sb.append("[");
for (Show show : 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();
return SUCCESS;
}

分享到:
评论

相关推荐

    SSH+Ajax+JSON省市级联效果Spring+Struts+Hibernate+Jquery

    Spring+Struts+Hibernate+Jquery(AJAX)实现的省份与城市级联效果 原理:通过ajax调用后端struts的action,将返回的JSON对象数组解析载入页面。

    ajax_struts_jquery_json级联菜单

    ajax struts jquery json 实现从数据库回调下拉菜单

    jQuery实现的多级级联下拉列表

    基于jQuery实现的多级级联下拉列表,例子中使用数据类型为JSON

    struts2 三级级联

    本人的处理方法,实现struts2的级联 包括配置文件 jsp中的jquery处理

    城市级联之三级联动

    该项目为一个整合hibernate与struts,利用json对象存放临时数据库数据,连接数据的三级联动;改项目使用ajax传递请求连接和参数;数据库结果为三表级联,三表之间依次都是多对一的关系;其中代码核心代码都附有详细...

    利用了jquery的ajax实现二级联互动菜单

    用到的包有:json-lib-2.2.3-jdk15.jar ezmorph-1.0.6.jar json.js jquery.js jsp页面的代码: 代码如下: &lt;&#37;@ page contentType=”text/html; charset=gbk”%&gt; &lt;&#37;@ taglib prefix=”s” uri=”/struts...

    jquery实现下拉菜单的二级联动利用json对象从DB取值显示联动

    利用struts2和Ajax实现json对象的传输,然后实现菜单的二级联动 下面是我的 js文件原码: 代码如下: var mail={ //初始化 init:{ //初始化数据 initdata:{ did:”, ttitle:”, sendpassword:”, description:” }, /...

    Java面试宝典2020修订版V1.0.1.doc

    6、struts2的核心组件有哪些? 89 7、Strus2的执行过程 89 8、为什么要使用struts2? 90 9、openSession和getCurrentSession 90 10、拦截器的作用?拦截器和过滤器的区别? 91 11、struts.xml中result的type有哪些...

    Java学习笔记-个人整理的

    \contentsline {chapter}{Contents}{2}{section*.1} {1}Java基础}{17}{chapter.1} {1.1}基本语法}{17}{section.1.1} {1.2}数字表达方式}{17}{section.1.2} {1.3}补码}{19}{section.1.3} {1.3.1}总结}{23}{...

    cascadeSelcte.rar

    编写了一个级联的下拉框,每次改变下拉框的值都会查询数据库,动态添加下拉框,里面用到了json数据格式,源文件是用Myeclipse工具编写的,导入后,发布到Tomcat直接可以运行,另外附带mysql数据库,直接把数据库导入...

Global site tag (gtag.js) - Google Analytics