使用 jQuery、ajax、struts2 根据第一个下拉列表选择填充第二个下拉列表
编辑 有没有其他方法可以根据第一个下拉选择 jquery ajax 填充第二个下拉列表。如果有人有链接,请发布任何链接。
我已根据第一个下拉列表选择更新了第二个下拉列表。我正在使用 jQuery 和 Struts2。我想使用 jQuery ajax 更新第二个下拉列表。有人可以帮我代码吗?我尝试使用下面的方法,但不知何故我无法将参数传递给 Action 类。先感谢您。
http ://www.joe-stevens.com/2010/02/23/populate-a-select-dropdown-list-using-jquery-and-ajax/
编辑: 根据 Climbage 所说,我正在使用我拥有的代码进行更新。
caseSelect 是第一个下拉菜单,termSelect 是第二个,casetermcodes 是操作。 selCaseDropDown 是隐藏变量,我想获取 Action 类中第一个下拉列表的选定值,其中我有该变量的 getter 和 setter 方法。我首先坚持将选定的值发送到操作类。至此我还没有做任何进一步的事情。
$("#caseSelect").change(
function(){
$("#result").html('Retrieving ...');
var selCase = $("#caseSelect").val();
$.ajax({
type: "POST",
url: "/dwdst/casetermcodes",
data: {selCaseDropDown: selCase},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$("#termSelect").get(0).options.length = 0;
$("#termSelect").get(0).options[0] = new Option("Term Codes", "-1");
$.each(msg.d, function(index, item) {
$("#termSelect").get(0).options[$("#termSelect").get(0).options.length] = new Option(item.value, item.key);
});
},
error: function() {
alert("Failed to load Term Codes");
$("#result").hide();
}
});
});
动作类:
public String execute(){
logger.info("selected value >>"+selCaseDropDown);
return SUCCESS;
}
public String getSelCaseDropDown(){
return selCaseDropDown;
}
public void setSelCaseDropDown(String selCaseDropDown){
this.selCaseDropDown = selCaseDropDown;
}
Edit Is there any other way I can fill the 2nd drop down as per 1st drop down selection jquery ajax. Please post any link if anyone has one.
I have update the second dropdown based on the first dropdown selection. I am using jQuery and Struts2. I want to update the second dropdown using jQuery ajax. Can someone please help me the code. I tried with the below method but somehow I was unable to pass the parameters to Action class. Thank you in advance.
http://www.joe-stevens.com/2010/02/23/populate-a-select-dropdown-list-using-jquery-and-ajax/
Edit:
As per Climbage said I am updating with code whatever I have.
caseSelect is first Dropdown, termSelect is second, casetermcodes is action. selCaseDropDown is hidden variable I want fetch the selected value of first dropdown in Action class where I have getter and setter methods for this variable. I first stuck at sending the selected value to action class. I have not did anything further this point.
$("#caseSelect").change(
function(){
$("#result").html('Retrieving ...');
var selCase = $("#caseSelect").val();
$.ajax({
type: "POST",
url: "/dwdst/casetermcodes",
data: {selCaseDropDown: selCase},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$("#termSelect").get(0).options.length = 0;
$("#termSelect").get(0).options[0] = new Option("Term Codes", "-1");
$.each(msg.d, function(index, item) {
$("#termSelect").get(0).options[$("#termSelect").get(0).options.length] = new Option(item.value, item.key);
});
},
error: function() {
alert("Failed to load Term Codes");
$("#result").hide();
}
});
});
Action class :
public String execute(){
logger.info("selected value >>"+selCaseDropDown);
return SUCCESS;
}
public String getSelCaseDropDown(){
return selCaseDropDown;
}
public void setSelCaseDropDown(String selCaseDropDown){
this.selCaseDropDown = selCaseDropDown;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过创建另一个简短的 JSP 解决了这个问题,该 JSP 仅作为 html 代码下拉(第二个下拉),并且在我的操作结果中将是这个短页面。
当第一页选择调用操作时,使用下面的代码并使用第二个简短的下拉菜单覆盖 jsp 中已显示的下拉菜单。
getSecondSelect
是我正在调用的操作,其结果为该短 jsp。tercodeResult
是被新下拉列表替换的 div 标签。selId
是从第一个下拉列表中选择的值。I have solved this by creating another short JSP which has only drop down (second drop down) as html code and in my action result would be this short page.
When the first page is select call action using below code and overriding the drop down already shown in jsp with this second short drop down only jsp.
getSecondSelect
is action I am calling which has result as that short jsp.tercodeResult
is the div tag which is replaced with new drop down.selId
is the selected value from first drop down.尝试一下,它将在 Struts 2.0.14 中使用 jsonplugin-0.32.jar 为您提供帮助。
struts.xml:
action类 Helloworld.java:
可以直接调用HelloWorld.action来查看JSON数据,也可以将JSON数据绑定到下面的表单元素上。
JSP页面HelloWorld.jsp:
Try this, will help you in Struts 2.0.14 with jsonplugin-0.32.jar.
struts.xml:
action class Helloworld.java:
You can directly call HelloWorld.action to view the JSON data or else you can bind the JSON data to a form element below.
JSP page HelloWorld.jsp: