jquery form插件是用于在页面使用ajax上传表单、文件,不刷新
来源:互联网 发布:wnba数据 编辑:程序博客网 时间:2024/06/06 21:45
jquery form插件是用于在页面使用ajax上传表单、文件,不刷新的功能,直接开始例子:
有两种方式: ajaxForm 和ajaxSubmit ,前者直接在文档加载完成之后绑定到form上即可,在页面提交的时候会自动触发里面的处理,后者表示手动调用插件的ajax提交时机,可以在form提交的时候触发ajaxSubmit,也可以在一个button的click事件里面触发ajaxSubmit
先使用ajaxSubmit:
页面:
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'upload.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <script type="text/javascript" src="<%=basePath %>common/js/jquery-1.8.2.min.js"></script>
- <script type="text/javascript" src="<%=basePath %>common/js/jquery.form.js"></script>
- <script type="text/javascript">
- $(document).ready(function() {
- */
- //在form提交的时候触发,这个触发的时机是自己控制的,例如也可以在一个button的click事件里面触发ajaxSubmit
- $("#myForm").submit(function(){
- $('#myForm').ajaxSubmit({
- beforeSubmit:handleBeforeSubmit,
- success:handleSuccess
- });
- //记得返回false,阻止页面的默认提交行为
- return false;
- });
- });
- /**
- * 提交请求发出之前的处理
- */
- function handleBeforeSubmit(){
- console.info('提交请求发出之前的处理')
- }
- /**
- * 上传成功后的处理
- */
- function handleSuccess(){
- console.info('上传成功后的处理')
- alert('上传成功 !');
- }
- </script>
- </head>
- <body>
- <form action="upload/uploadFile.htm" method="post" enctype="multipart/form-data" id="myForm">
- <label for="myFile">选择文件:</label>
- <input type="file" name="myFile" />
- <input type="hidden" name="myfield" value="myvalue">
- <input type="submit" value="上传"/>
- </form>
- </body>
- </html>
或者:
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'upload.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <script type="text/javascript" src="<%=basePath %>common/js/jquery-1.8.2.min.js"></script>
- <script type="text/javascript" src="<%=basePath %>common/js/jquery.form.js"></script>
- <script type="text/javascript">
- $(document).ready(function() {
- $('#myForm').ajaxForm({
- beforeSubmit:handleBeforeSubmit,
- success:handleSuccess
- });
- });
- /**
- * 提交请求发出之前的处理
- */
- function handleBeforeSubmit(){
- console.info('提交请求发出之前的处理')
- }
- /**
- * 上传成功后的处理
- */
- function handleSuccess(){
- console.info('上传成功后的处理')
- alert('上传成功 !');
- }
- </script>
- </head>
- <body>
- <form action="upload/uploadFile.htm" method="post" enctype="multipart/form-data" id="myForm">
- <label for="myFile">选择文件:</label>
- <input type="file" name="myFile" />
- <input type="hidden" name="myfield" value="myvalue">
- <input type="submit" value="上传"/>
- </form>
- </body>
- </html>
都是一样的效果,后台action(这里使用struts2)
struts.xml配置:
- <!-- 处理文件上传 -->
- <action name="uploadFile" class="uploadFileAction" method="uploadFile">
- </action>
后台Action:
- package com.tch.test.template.web.action;
- import java.io.File;
- import org.apache.commons.io.FileUtils;
- import org.springframework.context.annotation.Scope;
- import org.springframework.stereotype.Component;
- import java.io.IOException;
- import com.opensymphony.xwork2.ActionSupport;
- @Component("uploadFileAction")
- @Scope("prototype")
- public class UploadFile extends ActionSupport {
- private static final long serialVersionUID = 1L;
- private File myFile;
- private String myFileContentType;
- private String myFileFileName;
- private String destPath;
- private String myfield;
- public void uploadFile() {
- /* Copy file to a safe location */
- destPath = "e:/";
- try {
- System.out.println("Src File name: " + myFile);
- System.out.println("Dst File name: " + myFileFileName);
- System.out.println("myfield : "+myfield);
- File destFile = new File(destPath+myFileFileName);
- FileUtils.copyFile(myFile, destFile);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public File getMyFile() {
- return myFile;
- }
- public void setMyFile(File myFile) {
- this.myFile = myFile;
- }
- public String getMyFileContentType() {
- return myFileContentType;
- }
- public void setMyFileContentType(String myFileContentType) {
- this.myFileContentType = myFileContentType;
- }
- public String getMyFileFileName() {
- return myFileFileName;
- }
- public void setMyFileFileName(String myFileFileName) {
- this.myFileFileName = myFileFileName;
- }
- public String getMyfield() {
- return myfield;
- }
- public void setMyfield(String myfield) {
- this.myfield = myfield;
- }
- }
OK。
0 0
- jquery form插件是用于在页面使用ajax上传表单、文件,不刷新
- jQuery.form.js是一个form插件,支持ajax表单提交和ajax文件上传。
- 利用JQUERY AJAX上传文件。重点是实现不刷新页面
- 不刷新页面的PHP与Jquery AJAX文件上传
- 使用jQuery.form插件实现表单异步提交+上传文件
- 使用jQuery.form插件实现表单异步提交+上传文件
- Struts2 Ajax文件上传功能 使用Jquery.form插件
- 使用 jQuery Form 插件 Ajax 提交表单
- jquery form插件(三)ajax文件上传
- Jquery.form提交表单数据,不刷新页面。
- springmvc+jquery-form插件实现页面无刷新上传
- 将jQuery Pagination分页插件用于不使用AJAX加载数据的页面
- form表单,AJAX上传文件
- JQuery异步提交表单(不使用jQuery.form插件)
- 用jquery.form ajax提交文件上传表单
- 使用ajax提交form表单,包括ajax文件上传
- 使用ajax提交form表单,包括ajax文件上传
- 使用ajax提交form表单,包括ajax文件上传
- Perl 中的正则表达式
- 【转载】视觉跟踪综述
- 堆和栈的区别
- Eclipse的Aptana studio插件安装
- keytool生成keystore、truststore、证书以及SSL单向认证在服务端tomcat和客户端的配置
- jquery form插件是用于在页面使用ajax上传表单、文件,不刷新
- 数据提交成功但数据库中没有相应的数据
- Windows 负载均衡集群
- Python笔记——(一)
- Enum获取枚举值的方法
- 什么是微信商城(微商城),如何建设微商城
- Linux 下 Xdebug 的安装
- Unity3D的JS和C#脚本互调
- 要提高SQL查询效率where语句条件的先后次序应如何写使你的SQL 语句完全优化