如何在我的表单中将DropzoneJS图像字段设置为可选字段

4

我在表单中使用了dropzoneJS。除了图片外,表单还记录了用户的输入内容。一切都运行正常,用户的输入被存入了我的数据库,图片也上传到了指定的目录。 现在,我想要将拖放上传图片字段设置为可选字段,即用户可以选择上传图片也可以将其留空。但是问题在于,“提交”按钮在用户未拖放任何图片时无法正常工作。 因此,请建议我如何使“提交”按钮可以提交表单,即使用户没有选择任何图像。我不是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();
});
}
1个回答

2

您可以检查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
    }
  });
}

同时阅读this可能会对你有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接