2011-04-14 76 views
7

jQuery File Uploader:https://github.com/blueimp/jQuery-File-UploadjQuery,如何检查一个插件是否已经应用到一个div?

我正在使用上面的插件。如何在jQuery中检查是否已经应用了fileUpload?

我现在得到以下错误:

Uncaught FileUpload with namespace "file_upload" already assigned to this element 
jQuery.jQuery.extend._Deferred.deferred.resolveWithjquery-1.5.1.js:869 
donejquery-1.5.1.js:6591 
jQuery.ajaxTransport.send.callbackjquery-1.5.1.js:7382 

有没有办法我的函数前检查呼叫:

$('.upload').fileUploadUI({ 
......... 
......... 
......... 

感谢

回答

16

您可以添加/设置一个类作为各种旗帜。在这种情况下,我们将添加一个名为applied

//scroll through each upload item 
$('.upload').each(function(){ 

    //Make sure class is not found 
    if (!$(this).hasClass('applied')) 

     //Apply Class and Upload Plugin 
     $(this).addClass('applied').fileUploadUI({...}); 
}); 

更新类由yoavmatchulsky下面指出的那样,你也可以,更容易做

$('.upload:not(.applied)').addClass('applied').fileUploadUI({...}); 
+5

或使用$( '上传:不是(.applied)') .fileUploadUI(...) – yoavmatchulsky 2011-04-14 21:48:19

2

jQuery的文件上传插件存储引用它的FileUpload处理程序为jQuery data对象。
错误消息“FileUpload with namespace”file_upload“already assigned to this element”来自插件自己对此数据引用的检查。

可以初始化插件下面的方法来防止此错误:

$('.upload').not(function() { return $(this).data('file_upload'); }).fileUploadUI({/*...*/}); 
+0

谢谢,这解决了我的问题。 – 2012-10-24 15:41:35

2

你也可以做到这一点

if(undefined != $('.upload').data('blueimp-fileupload')){ 
    console.log('plugin already applied to the element'); 
} 
else console.log('plugin not applied to the element'); 
+1

这应该是答案。有时我们无法控制修改代码。 (想想,第三方代码的硒测试。) – 2016-12-20 19:54:58

+1

简单而美丽! – 2017-01-16 10:30:30

相关问题