5

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

HTML:

<a id="CP" style="cursor: pointer; text-decoration: underline;">Current Processes</a>

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.
        }
    });
});
4

6 回答 6

14

如果我理解得很好,您可以通过以下方式非常简单地做到这一点:

$this.html("Processing...").css({
    "cursor": "wait",
    "pointer-events": "none"
});
于 2015-11-16T21:26:20.130 回答
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
        }
    });
});
于 2015-05-09T22:43:37.310 回答
0

$this.on("click")- 这实际上是:“将点击处理程序绑定到 $this 将什么都不做”

要触发事件,请使用:$this.click()$this.trigger("customEvent")

于 2013-07-03T11:51:27.900 回答
0

可能是你应该使用bind("click"),所以你也可以解绑它。例如$("#CP").bind("click",function(){..function to run // disable the click event from CP $("#CP").unbind("click"); });。祝你好运!

于 2013-07-03T12:22:29.583 回答
0
var eventhandler = function() {

    $(document).unbind("click");
    $this.html("Processing...").css({
        "text-decoration": "none",
        "cursor": "default"
    });
    $.ajax({
        type: 'post',
        url: 'abc.jsp',
        success: function(process) {
            //My code goes here...
            //Here i want to bind or add handler which is fired previously.
            $(document).click(eventhandler); 
        }
    });
}


jQuery(document).click(eventhandler);
于 2014-09-05T09:50:00.967 回答
0

也许您在使用javaScript提交表单时遇到过一个常见问题,如果多次单击提交按钮,则表单数据会被多次提交。要解决这个问题,你只需要写一行代码,帮助CSS

document.getElementById("form1").style.pointerEvents="none";
于 2019-06-28T12:53:26.753 回答