0

I am developing an asp.net app in which I am using Jquery and I want if Jquery click function calls once then it should not be called again until a page load. The click function code is following

  $("#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);

                }
            });
        })

Sorry for poor English. Thanks and Regards

1

3 Answers 3

1

Use jQuery one function:

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

Description: Attach a handler to an event for the elements. The handler is executed at most once per element.

docs

Sign up to request clarification or add additional context in comments.

Comments

0

At the top of your clikc handler add this:

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

Comments

0

The other answers are generally correct, but keep in mind when you want to unbind the event. As soon as the user clicks it? or as soon as the ajax response returns successfully?

$("#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');

        }
    });


})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.