当前位置: 首页 > news >正文

Jquery 异步提交表单(post)

方法  $.post(url,params,function(data){});

表单的action,method属性都没有

input 的类型只能为button不能为submit只能为button,否则点击button会执行表单action,不会走jquery异步

前台代码

<script type="text/javascript">
$(function()
        {
 
 //异步提交表单
 $("#save").click(function(){
  
  $.post("${ctx}/order/save.action",$("#form1").serialize(),function(data){
   
   if(data=="true")
    {
    alert ("备注保存成功");
    }
   
  })
 })
 })
</script> 
 
 
 
 
 <form  id="form1"  >
  <input type="hidden" name ="oid" value="${orderVo.id}">
  <textarea  class="form-control" name ="remark" id ="text" rows="3" cols="140"  >${orderVo.remark}</textarea>
  
   <input id="save" class="btn btn-primary" type="button" value="保存" />


后台代码:

 public void save(){
  PrintWriter out=null;
  try {
   System.out.println(oid);
   System.out.println(remark);
   orderService.saveRemark(oid,remark);
   HttpServletResponse response=ServletActionContext.getResponse();
     out=response.getWriter();
     out.print(true);
     out.flush();
     out.close();
   
  } catch (Exception e) {
   
   e.printStackTrace();
   out.flush();
   out.close();
   out.println(0);
  }
  
  
 }



webx框架

velocity模板实现代码

分页bean

package com.alibaba.uyuni.biz.common.bo.dto;

import java.io.Serializable;

import com.alibaba.uyuni.common.enums.NumEnum;

/**
 * 类Page.java的实现描述:TODO 类实现描述
 * 
 * @author 杨冬 2014年8月21日 下午1:19:25
 */
public class UyuniPage<T> implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 8104777827863916550L;
    /** 每页显示记录数 **/
    private Integer           pageSize;
    /** 查询的集合 **/
    private T                 data;
    /** 总页数 **/
    private int               totalPage;
    /** 当前页,第几页 **/
    private Integer           pageIndex;
    /** 总记录数 **/
    private Integer           totalNum;
    /** 是否有下页 **/
    private boolean           hasNextPage      = false;
    /** 是否有上页 **/
    private boolean           hasPreviousPage  = false;

    /**
     * @param pageSize 每页条数
     * @param pageIndex 当前页,第几页
     * @param totalNum 总记录数
     * @param data 查询的集合
     */
    public UyuniPage(Integer pageSize, Integer pageIndex, Integer totalNum, T data){
        super();
        if (pageSize == null || pageSize == 0) {
            pageSize = NumEnum.FIVE.getValue();
        }
        if (pageIndex == null || pageIndex == 0) {
            pageIndex = NumEnum.ONE.getValue();
        }
        if (totalNum == null || totalNum == 0) {
            totalNum = 0;
        }
        this.pageSize = pageSize;
        this.data = data;
        // pageSize==0会报错
        this.totalPage = (totalNum % pageSize == 0) ? (totalNum / pageSize) : (totalNum / pageSize + 1);
        this.pageIndex = (pageIndex == 0) ? (1) : (pageIndex);
        this.totalNum = totalNum;
        this.hasNextPage = (this.totalPage > 1 && this.totalPage > this.pageIndex);
        this.hasPreviousPage = (this.pageIndex > 1);

    }

    /**
     * @return the pageSize
     */
    public Integer getPageSize() {
        return pageSize;
    }

    /**
     * @param pageSize the pageSize to set
     */
    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * @return the data
     */
    public T getData() {
        return data;
    }

    /**
     * @param data the data to set
     */
    public void setData(T data) {
        this.data = data;
    }

    /**
     * @return the totalPage
     */
    public int getTotalPage() {
        return totalPage;
    }

    /**
     * @param totalPage the totalPage to set
     */
    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    /**
     * @return the pageIndex
     */
    public Integer getPageIndex() {
        return pageIndex;
    }

    /**
     * @param pageIndex the pageIndex to set
     */
    public void setPageIndex(Integer pageIndex) {
        this.pageIndex = pageIndex;
    }

    /**
     * @return the totalNum
     */
    public Integer getTotalNum() {
        return totalNum;
    }

    /**
     * @param totalNum the totalNum to set
     */
    public void setTotalNum(Integer totalNum) {
        this.totalNum = totalNum;
    }

    /**
     * @return the hasNextPage
     */
    public boolean isHasNextPage() {
        return hasNextPage;
    }

    /**
     * @param hasNextPage the hasNextPage to set
     */
    public void setHasNextPage(boolean hasNextPage) {
        this.hasNextPage = hasNextPage;
    }

    /**
     * @return the hasPreviousPage
     */
    public boolean isHasPreviousPage() {
        return hasPreviousPage;
    }

    /**
     * @param hasPreviousPage the hasPreviousPage to set
     */
    public void setHasPreviousPage(boolean hasPreviousPage) {
        this.hasPreviousPage = hasPreviousPage;
    }

    /**
     * @return the serialversionuid
     */
    public static long getSerialversionuid() {
        return serialVersionUID;
    }

}

vm页面

  <script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>
    <script type="text/javascript">

        $(function()
        {
               $("#confirm").click(function(){
                var studentId=$("#studentId").val();
                if(studentId==""){
                    alert("新增学生");
                    $.ajax({
                        url:"/project/studentRpc/newStudent.json",
                        type:"post",
                        data:$('#studentform').serialize(),
                        dataType: 'json',
                        success:function(data){
                            var isAdd= data.content.successed;
                            if(isAdd==true){
                                alert("新增成功");
                                window.location.reload();
                            }
                        }
                    });
            
                }else{
                   //更新学生
                    $.ajax({
                        url:"/project/studentRpc/"+studentId+"/updateStudent.json",
                        type:"post",
                        data:$('#studentform').serialize(),
                        dataType: 'json',
                        success:function(data){
                            var isDeleted= data.content.successed;
                            if(isDeleted==true){
                                alert("更新成功");
                                window.location.reload();
                            }
                        }
                    });
                }
            })
        })
    </script>
</head>
<body>
#if(${studentDto.id})
修改
#else
新增
#end
<form id="studentform">
    <input  id="studentId" type="hidden" name="id" value="$!studentDto.id"><br>
    姓名<input type="text" name="name" value="$!studentDto.name"><br>
    年龄<input type="text" name="age" value="$!studentDto.age"><br>
    性别<input type="text" name="sex" value="$!studentDto.sex"><br>
    年级<input type="text" name="grade" value="$!studentDto.grade"><br>
    班级<input type="text" name="team" value="$!studentDto.team"><br>
    老师<input type="text" name="teacher" value="$!studentDto.teacher"><br>
</form>
<button id="confirm">提交</button>
</body>

buttom标签必须放到form标签外面,否则点击button会执行表单action,不会走绑定的异步事件


分页显示页面

#set($nextpage=$pb.pageIndex + 1)
#set($previouspage=$pb.pageIndex - 1)

#if($pb.hasPreviousPage == true)     
<a href="/project/student/studentshow.htm?pageIndex=$previouspage">上一页</a>
#end

#if($pb.hasNextPage == true) 
<a href="/project/student/studentshow.htm?pageIndex=$nextpage">下一页</a>
#end
<br>
#foreach(${studentDto} in ${result})
      <tr>
      <td>${studentDto.id}</td>
      <td>${studentDto.name}</td>
      <td>${studentDto.age}</td>
      <td>${studentDto.sex}</td>
      <td>${studentDto.grade}</td>
      <td>${studentDto.team}</td>
      <td>${studentDto.teacher}  </td>
      <td><a href="/project/student/form.htm?studentId=${studentDto.id}">编辑</a></td>
      <td><a href="javascript:void(0);" onclick = "return delStudent(${studentDto.id});">删除</a></td>
      </tr>
      <br>
#end      
<a href="/project/student/form.htm">新增</a>

</div>


vm做加减运算必须在set标签里面做,且运算符两边必须有空格

    #set($a=10)  
    #set($b=$a - 1)  
    ------------  
    <span>$b</span>

不能直接像freemaker一样 ${b-1} 

http://liu400liu.iteye.com/blog/1197466


参考文章:

http://1194867672-qq-com.iteye.com/blog/1945827

 

本文出自 “点滴积累” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1682600

相关文章:

  • vue 项目 对 echarts 组件 的封装使用
  • 用户故事——需求的占位符
  • 截取视频中的 ‘暂停’ 操作,并让他自动不暂停
  • SQL学习之HAVING过滤分组
  • 项目中 处理后端返回多数据值, 数组形式 展示tab 列表上
  • ES6第一篇
  • 前端开发中,会遇到字符串填充的问题,padStart() 和 padEnd()
  • VS.net 2013中使用Git建立源代码管理 版本管理
  • react 项目中,选中一条信息,自动带出对应的信息
  • Linux 命令用法
  • 计算选中数据的某项 数据总和
  • 前端处理数组 在 js中把已知对象循环遍历后再push新的数组中
  • scrapy 爬取 useragent
  • react 项目中,某需求需要根据返回数据的某条件禁止选择行
  • 函数式思维的小例子
  • hexo+github搭建个人博客
  • 【React系列】如何构建React应用程序
  • 【个人向】《HTTP图解》阅后小结
  • Angular2开发踩坑系列-生产环境编译
  • C++类的相互关联
  • centos安装java运行环境jdk+tomcat
  • DataBase in Android
  • extract-text-webpack-plugin用法
  • mysql中InnoDB引擎中页的概念
  • Netty源码解析1-Buffer
  • oschina
  • Python利用正则抓取网页内容保存到本地
  • 区块链分支循环
  • 使用 @font-face
  • 王永庆:技术创新改变教育未来
  • 微信公众号开发小记——5.python微信红包
  • 小程序 setData 学问多
  • 正则表达式小结
  • 仓管云——企业云erp功能有哪些?
  • 说说我为什么看好Spring Cloud Alibaba
  • 微龛半导体获数千万Pre-A轮融资,投资方为国中创投 ...
  • ​软考-高级-系统架构设计师教程(清华第2版)【第12章 信息系统架构设计理论与实践(P420~465)-思维导图】​
  • # 达梦数据库知识点
  • # 再次尝试 连接失败_无线WiFi无法连接到网络怎么办【解决方法】
  • ## 临床数据 两两比较 加显著性boxplot加显著性
  • ###项目技术发展史
  • (C语言版)链表(三)——实现双向链表创建、删除、插入、释放内存等简单操作...
  • (Note)C++中的继承方式
  • (分享)一个图片添加水印的小demo的页面,可自定义样式
  • (接口自动化)Python3操作MySQL数据库
  • (论文阅读22/100)Learning a Deep Compact Image Representation for Visual Tracking
  • (一)插入排序
  • (原創) X61用戶,小心你的上蓋!! (NB) (ThinkPad) (X61)
  • (转)程序员技术练级攻略
  • .a文件和.so文件
  • .NET Core跨平台微服务学习资源
  • .NET Framework 服务实现监控可观测性最佳实践
  • .NET Micro Framework初体验(二)
  • .NET 反射的使用
  • .NET/C# 在代码中测量代码执行耗时的建议(比较系统性能计数器和系统时间)...