【发布时间】:2017-02-16 06:27:15
【问题描述】:
我在 Rails 应用程序中使用 dropzone.js。 dropzone 是具有其他输入元素的表单的一部分。在页面加载时,包含表单的 div 被隐藏。单击按钮时,该 div 会向下滑动。然而,在这个阶段 Dropzone 没有被初始化。但是如果我刷新页面并再次单击相同的按钮,它绝对可以正常工作。
这是我的 dropzone 函数(在 document.ready 内)
function dropzoneControl() {
Dropzone.options.myDropzone = {
url: '/the_url',
autoProcessQueue: false,
uploadMultiple: true,
parallelUploads: 5,
maxFiles: 5,
maxFilesize: 3,
acceptedFiles: 'image/*',
addRemoveLinks: true,
headers: {
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
},
init: function() {
dzClosure = this;
document.getElementById("submit-button").addEventListener("click", function(e) {
e.preventDefault();
e.stopPropagation();
dzClosure.processQueue();
});
//send all the form data along with the files:
this.on("sendingmultiple", function(data, xhr, formData) {
formData.append("name", $("#name").val()); * * other data * *
});
//on successful upload
this.on("successmultiple", function(file, responseText) {
$('#name').val(""); * * clear the rest of the form * *
});
this.on("queuecomplete", function(file) {
this.removeAllFiles();
});
}
}
}
【问题讨论】:
-
尝试使用
ready()函数之外的dropzone选项,这应该可以解决问题。或者你可以在页面加载完构造函数后手动初始化dropzone,看看dropzonejs.com/#create-dropzones-programmatically,不要忘记在使用第二个选项之前设置Dropzone.autoDiscover = false。
标签: jquery ruby-on-rails dropzone.js