溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Ajax如何實(shí)現(xiàn)提交Form表單及文件上傳

發(fā)布時(shí)間:2021-09-01 11:32:34 來源:億速云 閱讀:132 作者:小新 欄目:web開發(fā)

這篇文章主要為大家展示了“Ajax如何實(shí)現(xiàn)提交Form表單及文件上傳”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Ajax如何實(shí)現(xiàn)提交Form表單及文件上傳”這篇文章吧。

前幾天,發(fā)現(xiàn)了一些小問題。我在寫后臺(tái)管理頁面時(shí),需要上傳一張圖片。于是我就用很普通的Form表單上傳有一段Json串和圖片文件;

Form表單上傳圖片只需要在<form>標(biāo)簽里加上enctype = 'multipart/form-data',這樣是可以上傳圖片的;

但問題來了,在我進(jìn)行用Form表單提交的時(shí)候直接跳出來提交返回值的頁面并且原先的頁面刷新;

這樣我們可以先到異步的Ajax可以實(shí)現(xiàn)局部刷新;

廢話不多說了 直接上代碼;

首先是html:

<form id = "form_insert" method = "post">
<table style = "font-size: 13px; margin: 13px auto;"> 
<tr>
<td style = "text-align: right;">類型</td>
<td>:&nbsp;&nbsp;<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>:&nbsp;&nbsp;<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;">開始時(shí)間</td>
<td>:&nbsp;&nbsp;<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;">結(jié)束時(shí)間</td>
<td>:&nbsp;&nbsp;<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>:&nbsp;&nbsp;<input id ="mem_Province" style = "width:150px" class = "easyui-combobox" data-options = "required:true"></td>
</tr>
<tr><td colspan="2" ></td>
</tr>
<tr>
<td >市</td>
<td>:&nbsp;&nbsp;<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>:&nbsp;&nbsp;<input id = "mem_Shop" style = "width:150px" class = "easyui-combobox" data-options = "required:true"></td>
</tr>
<tr><td colspan="2" ></td>
</tr>
<tr>
<td style = "text-align: right;">具體地址</td>
<td>:&nbsp;&nbsp;<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 >
<tr>
<td style = "text-align: right;">上傳圖片</td>
<td>:&nbsp;&nbsp;<input class = "easyui-filebox" name = 'photo' style = "width:153px" data-options = "required:true,prompt:'選擇上傳圖片',buttonText:'&nbsp;選&nbsp;擇&nbsp;'"></td>
<td><input type = 'text' id = "Item" name = 'item' style = "display:none;"></td>
</tr>
</table>
</form>
<div style = "text-align:right; padding:2px 5px;">
<a id = "sub" class = "easyui-linkbutton" data-options = "iconCls:'icon-ok'" href = "javascript:void(0)">
保存
</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a class = "easyui-linkbutton" data-options = "iconCls:'icon-quxiao'" href = "javascript:void(0)" onclick = "window_open($('#insert_form'), 'close')">
取消
</a>&nbsp;&nbsp;&nbsp;&nbsp;
</div>

以上是html代碼,為了方便大家copy,css直接在標(biāo)簽里了;

有很多朋友想問,為什么寫兩個(gè)form表單;

這是因?yàn)楦鶕?jù)后臺(tái)接收數(shù)據(jù)的需求,傳的是信息變成字符串和圖片;

首先把信息變成字符串;

再放到第二個(gè)Form表單里,細(xì)心地朋友發(fā)現(xiàn)在第二個(gè)form表單里<input>標(biāo)簽里style=“display:none”這是個(gè)隱藏的標(biāo)簽;

不錯(cuò)我是通過第一個(gè)form表單獲取的數(shù)據(jù)通過js變成字符串再放到隱藏的標(biāo)簽里;

這樣通過Ajax提交第二個(gè)Form表單就可以了;

js代碼:

$( '#sub' ).click( function () {
  var actTimeStart1 = $ ('#actstarttime') . datebox ('getValue');
  var actTimeStart = changeDateToLong(actTimeStart1);
  var actTimeEnd1 = $('#actendtime').datebox('getValue');
  var actTimeEnd = changeDateToLong(actTimeEnd1);
  if(actTimeStart != '' && actTimeEnd != '' && (actTimeStart - actTimeEnd > 0)){
    $.messager.alert('警告','結(jié)束時(shí)間不能小于開始時(shí)間!','error');
    return false;
  }
  else{
    if ($('#form_insert').form('validate')) {
      var actType = document.getElementById("acttype").value;
      var actName = document.getElementById("actname").value;
      var actArea = document.getElementById("actadd").value;
      var actTimeStart1 = $('#actstarttime').datebox('getValue');
      var actTimeStart = changeDateToLong(actTimeStart1);
      var actTimeEnd1 = $('#actendtime').datebox('getValue');
      var actTimeEnd = changeDateToLong(actTimeEnd1);
      var t2 = $('#mem_Shop').combobox('getValue');
      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', //http://www.cnblogs.com/jayxxxxxxx/
        type : "post",
        data : form, //第二個(gè)Form表單的內(nèi)容
        processData : false,
        contentType : false,
        error : function(request) {
        },
        success : function(data) {
          $('#box').datagrid('reload');
        }
      });
      window_open($('#insert_form'), 'close');
    }else {
      $.messager.alert('警告' , '信息不完整!' , 'error');
    }
  }
});

大家看到了我用了FormData方法,說真的這個(gè)在html5里實(shí)在是太好用了,上傳圖片都不用再寫enctype = 'multipart/form-data';

以上是“Ajax如何實(shí)現(xiàn)提交Form表單及文件上傳”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI