通过代码示例分析给大家介绍ajax实现页面局部跳转与结果返回,具体内容如下:
1、带有结果返回的提交过程
这里用一个提交按钮来演示,html代码为:
<input type="button" class="btn" value="提报" name="submit4" onclick="tibao();">
点击提报按钮后,通过ajax来实现跳转到action中处理,javascript代码为:
function tibao(){
var id="";
var url = <select:link page="/smokeplan.do?method=tibao&idset="/>+id;
$.ajax({url: url,
type: "get",
success: function(result) {
alert(result);
}
});}
action处理完成后,将返回的结果放到result中,在页面弹出提示信息;当然这里的action跳转是需要配置xml的。
后台java类处理过程为:
//提报
public void tibao(actionmapping mapping, actionform form,
httpservletrequest request, httpservletresponse response) throws exception {
string idset=request.getparameter("idset");
callhelper helper = initializecallhelper("l_smokeboxtibaowldan", form,request, false);
helper.setparam("bill_ids",idset);
helper.setparam("personid",getpersonid(request));
helper.execute();
printwriter write = response.getwriter();
write.print(helper.getoutput("message"));
write.close(); }
这里是通过一个sql语句对数据进行处理,返回一个message,并将信息打印到页面;
这里做的操作的结果是反映到response对应的位置,于是拿到属于response的流,而不是new一个出来。
也就是说我从那里跳转过来的,我这个信息就会返回到那里去。所以在js中就可以用result进行接收这个返回结果,并且用alert提示。
使用ajax如何实现页面跳转
示例代码如下:
项目当中采用了ajaxanywhere框架来实现ajax,效果不错,并且容易实现,但现在问题是即使页面实现了效果,业务上还需要提交表单,在这种情况下,即使点击提交后,它仍然会刷新你定义好的zone区域,这个时候,如果单纯的提交表单就不够了,我采取的方案是:
利用js这个强大的bs项目开发工具,自定义一个函数来解决上述问题:
function doguahao()
{
if(checkdata())
{
if(document.form1.result_flag.value=="0")
{
return false;
}
else
{
if(document.form1.checktype.value=="danganhao")
{
form1.action = "<%=formaction%>";
form1.submit();
}
if(document.form1.checktype.value=="xingming")
{
form1.action = parent.left.url2;
form1.submit();
}
if(document.form1.checktype.value=="shenfenzheng")
{
form1.action = "<%=formaction%>";
form1.submit();
}
}
} }
以上就是ajax教程之页面局部跳转与结果返回的详细内容。