首页 > 解决方案 > AJAX 在 textarea 和输入中提交多部分表单

问题描述

好吧,我尝试了很多 JS 代码来发布一个包含多个数据的表单 [2 个文件和 1 个文本区域],但它们效果不佳。

但是如何使用 AJAX 向 PHP发送非空表单数据?

<form method="post" enctype="multipart/form-data">
  <textarea id="acas"></textarea>
  <input id="uimage" type="file" name="image" accept=".png,.jpg,.gif"/>
  <input id="uaudio" type="file" name="audio" accept=".mp3"/>
  <input id="armes" style="display: none;" name="send" type="submit"/>
</form>

默认情况下,我使用下面的 JS 代码提交表单,但它会重新加载页面:

$("#acas").on('keydown', function(e) {
    if (e.key == "Enter") { 
      if (e.shiftKey) {

      } else {
        e.preventDefault();
        $("#armes").click();
      }
    }
});

标签: javascriptjqueryhtml

解决方案


使用此代码

  $(document).on('submit', 'form', function (e)
    {
        var form = new FormData(this);
        jQuery.ajax({
            url: "",
            method: 'POST',
            processData: false,
            contentType: false,
            dataType: "json",
            data: form,
            success: function (response)
            {
             }
        });
        return false;
      });

推荐阅读