简体   繁体   English

数据属性未更新,并且onclick事件仍然存在

[英]data-attribute not updating and onclick event still on it

I have a problem with this onclick event effect : http://jsfiddle.net/WL6DX/1/ 我对此onclick事件效果有疑问: http : //jsfiddle.net/WL6DX/1/

/* '#generateZip' */
$("#generateZip[data-readyzip='start']").click(function(event) {

        event.preventDefault();

        /* change style/css */
        $('#generateZip').addClass("disabled");
        $('#generateZip').html("<i class=\"fa fa-spinner fa-spin\"></i> in progress !");

        /* call the zip */
        jQuery.ajax({
        type: 'POST',
        url: 'http://www.monde-du-rat.fr/API/zipMC.php',
        timeout: 8000,
        dataType: 'text',
        data: {
            call: 'yes'
        },
        "success": function (jqXHR, textStatus, errorThrown) {
            console.log("AJAX success :) - statut " + textStatus);
            /* change attributs */
            $('#generateZip').attr('data-readyzip', 'yes');
            setTimeout(readyZip, 3000);
        },
        "error": function (jqXHR, textStatus, errorThrown) {
            console.log("AJAX fail :/ - statut " + textStatus);
            /* change attributs */
            $('#generateZip').attr('data-readyzip', 'no');
            setTimeout(readyZipFAIL, 3000);
        }
        });

});

/* readyZip() */
function readyZip() {
    $('#generateZip').removeClass("disabled btn-primary");
    $('#generateZip').addClass("btn-success");
    $('#generateZip').html("download is ready !");
    $('#generateZip').attr("href", "http://www.monde-du-rat.fr/resources/data/documentMC.zip")
}

/* readyZipFAIL() */
function readyZipFAIL() {
    $('#generateZip').removeClass("btn-primary");
    $('#generateZip').addClass("btn-danger");
    $('#generateZip').html("problem");
}

even if data-readyzip is updated to "yes" value, the onclick event is still on it and i fail to download my file ... what's wrong ? 即使将data-readyzip更新为“是”值,onclick事件仍然存在,并且我无法下载文件...出了什么问题?

This code: 这段代码:

$("#generateZip[data-readyzip='start']").click(function...

finds all of the relevant elements as of when it was run (and hooks up a click handler to them). 找到所有相关的元素,当它运行 (与单击处理程序挂接到它们) It doesn't recheck them again later when you change things. 以后您进行更改时,不会再重新检查它们。

Two options: 两种选择:

  1. If you like, you can use event delegation instead: 如果愿意,可以改用事件委托:

     $(document).on("click", "#generateZip[data-readyzip='start']", function... 

    That hooks the click event on document , and then checks to see if it passed through any elements matching the selector while bubbling up to document from the element on which the event originated. 这会将click事件挂在document ,然后检查它是否通过与选择器匹配的任何元素传递,同时从发生该事件的元素冒泡到document

    document is, of course, a very deep level to be handling this. 当然, document是处理此问题的一个很深的层次。 You might want to hook the event on a container closer to the element. 您可能希望将事件挂在靠近元素的容器上。

  2. Since there's just one button, you can hook click on the button but only act on the click if it has the relevant attribute: 由于只有一个按钮,因此您可以钩住该按钮的单击,但只有在具有相关属性的情况下,才能对单击进行操作:

     $("#generateZip").click(function(e) { if ($(this).attr("data-readyzip") !== "start") { // Don't do it; prevent default or cancel bubbling if you like return; } // ... 

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

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