javascript - 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();
}
}
});
解决方案
使用此代码
$(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;
});
推荐阅读
- spring-boot - 在docker容器中运行spring应用程序时变音符号不起作用
- nginx - nginx在同一个域中配置多台服务器
- python - 带有配额cmd的python子进程输出
- c++ - 如何在 Python 中使用 Microsoft MIP SDK?
- json - I want to fetch data from txt file to json format
- browser - Opera 浏览器如何执行广告拦截?
- java - Spring Boot @ApiController 注释不起作用
- prometheus - 在 Grafana 中将标签转换为 X 轴
- javascript - 在选择框中添加项目
- dns - Gmsh - 您可以通过仅垂直拆分而不是拆分所有元素来进行网格细化吗?