单击按钮后如何在Dropzone.js中调用.removeAllFiles()函数
我正在使用http://www.dropzonejs.com/(dropzone.js )库。 我初始化了我的一个表单元素,
var drop = $("#upload").dropzone({ uploadMultiple: "true", addRemoveLinks: "true", thumbnailWidth: "250", thumbnailHeight: "250", maxFilesize: "10", headers: { "title": titles, "location": locations, "tag": tags } });
现在当用户点击按钮时,我想使用
drop.removeAllFiles(true)
函数清空整个列表,如Dropzone.js官方网站上所建议的那样。
所以,我试过用
$("#close").click(function(){ drop.removeAllFiles(true); });
但是它没有用,在console.log
我收到错误removeAllFiles() is not declared for drop
。
我哪里错了?
这对我有用。
Dropzone.forElement("#YourDropBoxId").removeAllFiles(true);
参考: https : //github.com/enyo/dropzone/issues/180
这是代码,它将解决您的问题。
$(function() { Dropzone.options.myAwesomeDropzone = { init: function () { var myDropZone = this; $("#btnRemoveAll").click(function () { myDropZone.removeAllFiles(); } ); } }; });
您的drop
引用了jQuery对象而不是Dropzone对象。
var drop = $("#upload").dropzone(options);
有时需要使用带有dropzone的jQuery(selector [,context]) 。 所以非jquery构造函数不那么方便。
var drop = new Dropzone(selector, options);
相反,您可以使用(丑陋)获取Dropzone对象:
var drop = $("#upload").dropzone(options).get(0).dropzone; $("#close").click(function(){ drop.removeAllFiles(true); });
这是有效的…请试试这个。
$("#close").click(function(){ drop.removeAllFiles(true); }); $("#upload").dropzone({ uploadMultiple: "true", addRemoveLinks: "true", thumbnailWidth: "250", thumbnailHeight: "250", maxFilesize: "10", headers: { "title": titles, "location": locations, "tag": tags }, init: function () { var dropzone = this; $("#close").click(function(){ dropzone.removeAllFiles(true); }); } });
试试这个。 这还包括对服务器的JSON调用以删除文件。
mydropzone = new Dropzone("#mydropzone",{ url: "/dropzone", addRemoveLinks : true, maxFilesize: 2.0, dictResponseError: 'Error uploading file!', removedfile: function(file) { var name = {file: file.name} $.ajax({ type: 'POST', url: '/dropzone_delete', contentType: 'application/json', data: JSON.stringify(name), dataType: 'json', success: function (e) {console.log(e);} }); var _ref; return (_ref = file.previewElement) != null ? _ref.parentNode.removeChild(file.previewElement) : void 0; } }); // Clear all files $("#btn_clear_dropzone").click(function () { // Delete existing files mydropzone.removeAllFiles(); // Cancel current uploads mydropzone.removeAllFiles(true); });
如果有人建议您进一步完善此代码,请与我们联系。