使用jQuery启用/禁用元素的Click事件

Ric*_*ock 3 jquery events

我想在元素上启用/禁用单击事件.我有以下代码......

HTML:

<a id="CP" style="cursor: pointer; text-decoration: underline;">Current Processes</a>
Run Code Online (Sandbox Code Playgroud)

jQuery的:

$(document).on("click", "#CP", function(event) {
    $this = $(this);
    $this.click(function() {
        return false;
    });
    $this.html("Processing...").css({
        "text-decoration": "none",
        "cursor": "default"
    });
    $.ajax({
        type: 'post',
        url: 'abc.jsp',
        success: function(process) {
            //My code goes here...
            $this.on("click"); //  Here i want to bind or add handler which is fired previously.
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

Jer*_*ans 9

如果我理解得很好,你可以用以下方法做到这一点:

$this.html("Processing...").css({
    "cursor": "wait",
    "pointer-events": "none"
});
Run Code Online (Sandbox Code Playgroud)


Rus*_*nas 7

在执行 AJAX 请求时添加一些类。完成后删除。如果链接有这个类,则什么都不做。

$(document).on("click", "#CP", function(event) {
    event.preventDefault();
    $a = $(this);
    if($a.hasClass('disabled')) {
        return false;
    }

    $a.html("Processing...").addClass('disabled');
    $.ajax({
        type: 'post',
        url: 'abc.jsp',
        success: function(process) {
            $a.removeClass('disabled');

            // restore inner HTML here
        }
    });
});
Run Code Online (Sandbox Code Playgroud)