简体   繁体   English

Jquery ReEnable 禁用按钮

[英]Jquery ReEnable disable button

For file upload, I've used a WordPress plugin.对于文件上传,我使用了 WordPress 插件。

Here I want to control the maximum number of file upload.这里我要控制文件上传的最大数量。 I'm able to disable the upload plugin when user upload the max number file.当用户上传最大数量的文件时,我可以禁用上传插件。
But after disabling the upload button, when the user delete a file and want to upload a new file he can't.但是禁用上传按钮后,当用户删除文件并想要上传新文件时,他不能。 How do I reEnable the disbale upload button?如何重新启用禁用上传按钮? I've disabled the upload button, count with the total number of input class.我禁用了上传按钮,用输入类的总数计算。

jQuery(document).ready(function() {
    var limit = 3;
    jQuery('.qbutton').on('click', function() {
        if (jQuery(".mfcf7-zl-multifile-name").length == limit) {
            jQuery(this).attr("disabled", "disabled");
        }
    });
    jQuery('.mfcf7_zl_delete_file').on('click', function() {
        if (jQuery(".mfcf7-zl-multifile-name").length < limit) {
            jQuery('.qbutton').removeAttr("disabled");
        }
    });
});

update: Event listener not work for dynamically create class .mfcf7_zl_delete_file更新:事件侦听器不适用于动态创建类.mfcf7_zl_delete_file
After removing attach file click event not trigged.删除附件文件点击事件后未触发。

I believe you just need to update your condition: 我相信您只需要更新您的状况即可:

jQuery(document).on('click', '.mfcf7_zl_delete_file', function() {
    // do the following stuff after deleting the file
    if (jQuery(".mfcf7-zl-multifile-name").length < limit) {
        jQuery('.qbutton').removeAttr("disabled");
    }
});

I think, Your on click event is not getting triggered. 我认为,您的点击事件不会被触发。 Also need to re-check the condition. 还需要重新检查条件。

jQuery(document).on('click', 'mfcf7_zl_delete_file', function(){ 
     if (jQuery(".mfcf7-zl-multifile-name").length < limit) {
            jQuery('.qbutton').removeAttr("disabled");
        }
});

the element no yet exists you have to add listener from the dom selector try this 该元素尚不存在,您必须从dom选择器中添加监听器

var $file = jQuery(".mfcf7-zl-multifile-name");
jQuery(document).on('click', $file, function(){
   if ($file.length < limit) {
        jQuery('.qbutton').removeAttr("disabled");
   }
}) 

you can use document or any parent element 您可以使用文档或任何父元素

Since the target element doesn't exist on the page when the document is ready no event is attached to the elements that are created after selecting files for upload. 由于document准备好后页面上不存在目标元素,因此在选择要上传的文件后创建的元素不会附加任何事件。 Use delgation like 使用像

// $('container element').on('trigger name', 'dynamic target', function(e) {
    // do something
// });

jQuery('.mfcf7_zl_multifilecontainer').on('click', '.mfcf7_zl_delete_file', function(e) {
    if (jQuery(".mfcf7-zl-multifile-name").length >= limit) {
        jQuery('.qbutton').prop("disabled", false);
    }
});

This will capture clicks on any .mfcf7_zl_delete_file that were added to .mfcf7_zl_multifilecontainer after the page was loaded. 这将捕获在加载页面后添加到.mfcf7_zl_multifilecontainer任何.mfcf7_zl_delete_file单击。

By the way, notice that I used .prop() instead of working with attr() , see my comment on your question for details. 顺便说一句,请注意我使用了.prop()而不是attr() ,有关详细信息,请参见我对您的问题的评论。

EDIT: Appears like wordpress adds elements dynamically. 编辑:看起来像wordpress动态添加元素。 I have not much knowledge of wordpress. 我对Wordpress的了解不多。 Try this: 尝试这个:

jQuery(document).ready(function() {
    var limit = 3;
        jQuery(document).on('change', '.mfcf7_zl_multifilecontainer', function() {
        if(jQuery('body').find(".mfcf7-zl-multifile-name").length >= limit) {
            jQuery('body').find('.qbutton').attr("disabled", "disabled");
        } else {
            jQuery('body').find('.qbutton').removeAttr("disabled");
        }
    });
});

You can use an element (must be in the DOM path to body from your .qbutton ) that you know for sure exists on page load instead of body in above code 您可以使用一个元素(必须在DOM路径body从你的.qbutton ,你肯定知道在页面加载,而不是存在) body在上面的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM