jQuery dropzone.js 在没有 dropzone 的页面上给出错误“无效的 dropzone 元素”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27870237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
dropzone.js is giving error "Invalid dropzone element" on page without a dropzone
提问by user1048676
I'm using dropzone js and it's working great on pages that I require a dropzone. On any other page though it's giving me a "Invalid dropzone element"
error message and causing issues with my other javascript.
我正在使用 dropzone js,它在需要 dropzone 的页面上效果很好。在任何其他页面上,虽然它给我一条"Invalid dropzone element"
错误消息并导致我的其他 javascript 出现问题。
I have a custom JS file (which loads immediately after the dropzone.js file) and at the very top of the file I have the following line of code:
我有一个自定义 JS 文件(在 dropzone.js 文件之后立即加载)并且在文件的最顶部我有以下代码行:
Dropzone.autoDiscover = false;
This should stop it from looking at any page where I'm not enabling it programatically. The error only goes away on pages where there is a valid dropzone.
这应该阻止它查看我没有以编程方式启用它的任何页面。该错误仅在存在有效放置区的页面上消失。
I also set the following code on line 1470 on dropzone.js to try and enable it there too:
我还在 dropzone.js 的第 1470 行设置了以下代码,以尝试在那里启用它:
Dropzone.autoDiscover = false;
How can I get rid of this error?
我怎样才能摆脱这个错误?
回答by Philip
With pure JS you can try this:
使用纯 JS 你可以试试这个:
if (document.getElementById('DropzoneElementId')) {
var myDropzone = new Dropzone("div#DropzoneElementId", { url: "/file/post"});
// other code here
}
or if you use jQuery:
或者如果您使用 jQuery:
if ($('#DropzoneElementId').length) {
$("div#DropzoneElementId").dropzone({ url: "/file/post" });
// other code here
}
回答by VZimmerl
Long time ago but: Put the script after your Form Element and the error is gone.
很久以前但是:将脚本放在表单元素之后,错误就消失了。