首页 >web前端 >js教程 >Ajax提交Form表单及文件上传的实例代码

Ajax提交Form表单及文件上传的实例代码

韦小宝
韦小宝原创
2018-05-14 15:09:533036浏览

前段时间在做程序的时候发现一个小问题,在写后台管理Form表单上传图片时,在我进行用Form表单提交的时候直接跳出来提交返回值的页面并且原先的页面刷新,下面小编通过分享Ajax提交Form表单及文件上传的实例代码给大家解析下

前几天,发现了一些小问题。我在写后台管理页面时,需要上传一张图片。于是我就用很普通的Form表单上传有一段Json串和图片文件;

Form表单上传图片只需要在ff9c23ada1bcecdd1a0fb5d5a0f18437标签里加上enctype = 'multipart/form-data',这样是可以上传图片的;

但问题来了,在我进行用Form表单提交的时候直接跳出来提交返回值的页面并且原先的页面刷新;

这样我们可以先到异步的Ajax可以实现局部刷新;

废话不多说了 直接上代码;

首先是html:

<form id = "form_insert" method = "post">
<table style = "font-size: 13px; margin: 13px auto;"> 
<tr>
<td style = "text-align: right;">类型</td>
<td>:  <input id = "acttype" style = "width:150px" class = "easyui-textbox" data-options = "required:true"></td>
</tr>
<tr><td colspan = "2" style = "height: 13px"></td>
</tr>
<tr>
<td style = "text-align: right;">名称</td>
<td>:  <input id = "actname" style = "width:150px" class = "easyui-textbox" data-options = "required:true"></td>
</tr>
<tr><td colspan = "2" style = "height: 13px"></td>
</tr>
<tr>
<td style = "text-align: right;">开始时间</td>
<td>:  <input id = "actstarttime" style = "width:150px" class = "easyui-datetimebox" data-options = "required:true"></td>
</tr>
<tr><td colspan = "2" style = "height: 13px"></td>
</tr>
<tr>
<td style = "text-align: right;">结束时间</td>
<td>:  <input id = "actendtime" style = "width:150px" class = "easyui-datetimebox" data-options = "required:true"></td>
</tr>
<tr><td colspan = "2" style = "height: 13px"></td>
</tr>
<tr>
<td style = "text-align: right;">省</td>
<td>:  <input id ="mem_Province" style = "width:150px" class = "easyui-combobox" data-options = "required:true"></td>
</tr>
<tr><td colspan="2" style="height: 13px"></td>
</tr>
<tr>
<td style="text-align: right;">市</td>
<td>:  <input id = "mem_City" style = "width:150px" class = "easyui-combobox" data-options = "required:true"></td>
</tr>
<tr><td colspan = "2" style = "height: 13px"></td>
</tr>
<tr>
<td style = "text-align: right;">门店</td>
<td>:  <input id = "mem_Shop" style = "width:150px" class = "easyui-combobox" data-options = "required:true"></td>
</tr>
<tr><td colspan="2" style="height: 13px"></td>
</tr>
<tr>
<td style = "text-align: right;">具体地址</td>
<td>:  <input id = "actadd" style = "width:150px" class = "easyui-textbox" data-options = "required:true"></td>
</tr>
</table>
</form>
<form id = "form_sub" style = "font-size: 13px;">
<table style="font-size: 13px; margin: 13px auto;">
<tr>
<td style = "text-align: right;">上传图片</td>
<td>:  <input class = "easyui-filebox" name = &#39;photo&#39; style = "width:153px" data-options = "required:true,prompt:&#39;选择上传图片&#39;,buttonText:&#39; 选 择 &#39;"></td>
<td><input type = &#39;text&#39; id = "Item" name = &#39;item&#39; style = "display:none;"></td>
</tr>
</table>
</form>
<p style = "text-align:right; padding:2px 5px;">
<a id = "sub" class = "easyui-linkbutton" data-options = "iconCls:&#39;icon-ok&#39;" href = "javascript:void(0)">
保存
</a>    
<a class = "easyui-linkbutton" data-options = "iconCls:&#39;icon-quxiao&#39;" href = "javascript:void(0)" onclick = "window_open($(&#39;#insert_form&#39;), &#39;close&#39;)">
取消
</a>    
</p>

以上是html代码,为了方便大家copy,css直接在标签里了;

有很多朋友想问,为什么写两个form表单;

这是因为根据后台接收数据的需求,传的是信息变成字符串和图片;

首先把信息变成字符串;

再放到第二个Form表单里,细心地朋友发现在第二个form表单里d5fd7aea971a85678ba271703566ebfd标签里style=“display:none”这是个隐藏的标签;

不错我是通过第一个form表单获取的数据通过js变成字符串再放到隐藏的标签里;

这样通过Ajax提交第二个Form表单就可以了;

js代码:

$( &#39;#sub&#39; ).click( function () {
  var actTimeStart1 = $ (&#39;#actstarttime&#39;) . datebox (&#39;getValue&#39;);
  var actTimeStart = changeDateToLong(actTimeStart1);
  var actTimeEnd1 = $(&#39;#actendtime&#39;).datebox(&#39;getValue&#39;);
  var actTimeEnd = changeDateToLong(actTimeEnd1);
  if(actTimeStart != &#39;&#39; && actTimeEnd != &#39;&#39; && (actTimeStart - actTimeEnd > 0)){
    $.messager.alert(&#39;警告&#39;,&#39;结束时间不能小于开始时间!&#39;,&#39;error&#39;);
    return false;
  }
  else{
    if ($(&#39;#form_insert&#39;).form(&#39;validate&#39;)) {
      var actType = document.getElementById("acttype").value;
      var actName = document.getElementById("actname").value;
      var actArea = document.getElementById("actadd").value;
      var actTimeStart1 = $(&#39;#actstarttime&#39;).datebox(&#39;getValue&#39;);
      var actTimeStart = changeDateToLong(actTimeStart1);
      var actTimeEnd1 = $(&#39;#actendtime&#39;).datebox(&#39;getValue&#39;);
      var actTimeEnd = changeDateToLong(actTimeEnd1);
      var t2 = $(&#39;#mem_Shop&#39;).combobox(&#39;getValue&#39;);
      var jsonObj = {actType:actType,actName:actName,actTimeStart:actTimeStart,actTimeEnd:actTimeEnd,actArea:actArea,t2:t2};
      var activityMemberJson = JSON.stringify(jsonObj);
      document.getElementById("Item").value=activityMemberJson;
      var form = new FormData(document.getElementById("form_sub"));
      $.ajax({
        url : ../activity/actionActivityInsert&#39;, //http://www.cnblogs.com/jayxxxxxxx/
        type : "post",
        data : form, //第二个Form表单的内容
        processData : false,
        contentType : false,
        error : function(request) {
        },
        success : function(data) {
          $(&#39;#box&#39;).datagrid(&#39;reload&#39;);
        }
      });
      window_open($(&#39;#insert_form&#39;), &#39;close&#39;);
    }else {
      $.messager.alert(&#39;警告&#39; , &#39;信息不完整!&#39; , &#39;error&#39;);
    }
  }
});

大家看到了我用了FormData方法,说真的这个在html5里实在是太好用了,上传图片都不用再写enctype = 'multipart/form-data';

以上所述是小编给大家介绍的Ajax提交Form表单及文件上传的实例代码,希望对大家有所帮助!

相关推荐:

使用html5的FormData对象,通过 Ajax表单异步提交文件数据

JQuery打造PHP的AJAX表单提交实例

实现Ajax表单验证实例

以上是Ajax提交Form表单及文件上传的实例代码的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn