[英]Not able to add event handler in dropzone
我正在按鈕點擊調用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);});
}
};
}
不知道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);
}
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.