我在表单中使用dropzoneJS。除了图像,表单还记录用户输入。一切都很好。用户输入将进入我的数据库,图像将上传到定义的目录中。现在我想让drag-n-drop图像字段作为可选字段,用户可以选择上传图像,也可以将其留空。但问题是,"提交"按钮直到用户放下图像后才起作用。所以请建议我如何制作提交按钮来提交表单,即使用户没有选择任何图像。我不是javascript专家。
这是我的表格和按钮-
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" class="dropzone form-horizontal" id="my-awesome-dropzone" method="post" enctype="multipart/form-data">
<input type="text" ..blah blah
<input type .. blah blah
<!-- DropzoneJS div -->
<div class="dropzone-previews form-group" style="height: 350px;"></div>
<div class="fallback col-xs-3">
<input name="file" class="input-file form-control "type="file" multiple/>
</div>
<button type="submit" id="submit-dz" class="btn btn-primary" name="signup">Submit</button>
这就是javascript(在设置了必要的dropzone配置之后,例如autoProcessQueue:false和blah…blah)-
init: function() {
var myDropzone = this;
// Upload images when submit button is clicked.
$("#submit-dz").click(function (e) {
e.preventDefault();
e.stopPropagation();
myDropzone.processQueue();
});
}
您可以检查dropzone是否有文件要上传,并相应地提交表单:
init: function() {
var myDropzone = this;
$("#submit-dz").click(function (e) {
e.preventDefault();
e.stopPropagation();
if (myDropzone.files.length) {
myDropzone.processQueue(); // upload files and submit the form
} else {
$('#my-awesome-dropzone').submit(); // submit the form
}
});
}
阅读这篇文章也可能对你有所帮助。