我在按钮点击时调用这个addDropzone()函数,它传递一个值,因为需要mutilple dropzone元素,但是只有一个在时间但在不同的div上,所以函数中的'val'参数有助于定义div id。我能够运行dropzone元素。它还将文件保存在服务器上,但我无法使事件处理程序正常工作。
这是我正在使用的功能。
此外,如果有人可以建议我在事件处理程序中读取服务器消息的方法
function addDropzone(val){
document.getElementById("div2").innerHTML = '<div class="item" style="width:96%; margin:5px auto; position:relative; height: auto; background-color:lightgray;"><form action="file-upload.php" class="dropzone" id="my-dropzone-'+val+'"></form></div>';
var myDropzone = new Dropzone("div #my-dropzone-"+val);
Dropzone.options.myDropzone = {
paramName: 'file',
method: 'post',
maxFiles: 1,
url: 'file-upload.php',
dictDefaultMessage: "Drag your images",
clickable: false,
maxFilesize: 512,
uploadMultiple: false,
addRemoveLinks: true,
forceFallback:true
};
Dropzone.options.myDropzone = false;
Dropzone.options.myDropzone.enable();
Dropzone.options.myDropzone = {
init: function(){
this.on("maxfilesexceeded", function(file) { alert("File limit 1"); });
this.on("complete", function(file) {alert(serverReponse);});
}
};
}
答案 0 :(得分:0)
我不知道dropzone,但据我理解文档,我会这样写:
function addDropzone(val){
var dropZoneId= "my-dropzone-"+val;
// this line has moved-up, and the option is not referenced in the same way
Dropzone.options[dropZoneId] = false;
// proper way of adding new content to div2 without losing event handlers
var div = document.createElement('div');
div.innerHTML = '<div class="item" style="width:96%; margin:5px auto; position:relative; height: auto; background-color:lightgray;"><form action="file-upload.php" class="dropzone" id="'+dropZoneId+'"></form></div>';
document.getElementById("div2").appendChild(div.firstChild);
var myDropzone = new Dropzone("#"+dropZoneId,{
paramName: 'file',
method: 'post',
maxFiles: 1,
url: 'file-upload.php',
dictDefaultMessage: "Drag your images for zone "+val,
clickable: false,
maxFilesize: 512,
uploadMultiple: false,
addRemoveLinks: true,
forceFallback:false,
init: function(){
// from the doc, quick debug
this.on("addedfile", function(file) { alert("added file in zone."+val); });
this.on("maxfilesexceeded", function(file) { alert("File limit 1"); });
this.on("complete", function(file) {alert(serverReponse);});
}
});
}
$(document).ready(function(){
addDropzone(1);
addDropzone(2);
}
);