Jquery单击函数不应再次调用

Jquery click function should not call again

本文关键字:调用 单击 函数 Jquery      更新时间:2023-09-26

我正在开发一个asp.net应用程序,我在其中使用Jquery,我希望如果Jquery点击函数调用一次,那么在页面加载之前不应该再次调用它。点击功能代码在之后

  $("#plblAgreeProblem").click(function(){
      var str = {
            problemID: $("#problemID").val(),
            empID : $("#empID").val()
      }
      $.ajax({
                type: "POST",
                url: "<%= Url.Action("AgreeProblem", "Discussion")  %>",
                data: str,
                error: function(msg){
                    alert("error2" + msg);
                },
                success: function (msg) {
                    $("#ptxtDisAgreeProblem").empty();
                    $("#ptxtDisAgreeProblem").text(msg);
                }
            });
        })

抱歉英语不好。感谢和问候

使用jQuery one函数:

$("#plblAgreeProblem").one('click', function(){ //...

描述:将处理程序附加到元素的事件。每个元素最多执行一次处理程序。

docs

在clikc处理程序的顶部添加以下内容:

$("#plblAgreeProblem").unbind('click');

其他答案通常是正确的,但请记住您想解除事件绑定时。用户一点击它?还是在ajax响应成功返回之后?

$("#plblAgreeProblem").click(function() {
    var $this = $(this);
    var str = {
        problemID: $("#problemID").val(),
        empID: $("#empID").val()
    }
    $.ajax({
        type: "POST",
        url: "<%= Url.Action("
        AgreeProblem ", "
        Discussion ")  %>",
        data: str,
        error: function(msg) {
            alert("error2" + msg);
        },
        success: function(msg) {
            $("#ptxtDisAgreeProblem").empty();
            $("#ptxtDisAgreeProblem").text(msg);
            // do this unbind here, after the call has been made successfully.
            $this.unbind('click');
        }
    });

})