8

I am trying to achieve the scenario of disabling a button on clicking it, and then enabling it again after the ajax request has completed successfully.

The below is the snippet of my code.

Form

<button id="btnSubmit" type="submit" class="btn btn-warning btn-lg">Submit!</button>

Javascript

$('#resForm').validate({
  // disable submit button
  $('#btnSubmit').prop('disabled', true);

  submitHandler: function(form) {
    $.ajax({
      ..... typical ajax stuff .....
      success: function(data) {
        alert(data);
        $('success').html(data);
        // enable reserve button again
        $('#btnSubmit').prop('disabled', false);
      },
      error: function (jqXHR, textStatus, errorThrown) {
        // console.log(jqXHR);
      }
    });
  }
});

It doesn't work. And checking my console on Chrome tells me Uncaught SyntaxError: Unexpected token (. It feels like it's a stupid mistake somewhere and I can't figure it out. I have read that prop is meant for jQuery 1.6.1 and later and I am on 1.8.3.

1
  • Does the error specifies any line?Just try to run in firefox with the help of firebug..it will show you the exact problem Commented Oct 7, 2013 at 8:49

3 Answers 3

10
$('#resForm').validate({


  submitHandler: function(form) {
// disable submit button
  $('#btnSubmit').prop('disabled', true);
    $.ajax({
      ..... typical ajax stuff .....
      success: function(data) {
        alert(data);
        $('success').html(data);
        // enable reserve button again
        $('#btnSubmit').prop('disabled', false);
      },
      error: function (jqXHR, textStatus, errorThrown) {
        // console.log(jqXHR);
      }
    });
  }
});

Try this

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

1 Comment

see .validate() api it has a definite structure... u cannot add .prop in there...
2

From my answer to another post in SO! I can't think a simpler/easier way! ;-)


For Anchor Tags(Links) :

<a href="#delete-modal" class="btn btn-danger" id="delete"> Delete</a>

To enable the Anchor tag:

 $('#delete').removeClass('disabled');
 $('#delete').attr("data-toggle", "modal");

enter image description here


To disable the Anchor tag:

 $('#delete').addClass('disabled');
 $('#delete').removeAttr('data-toggle');

enter image description here

2 Comments

Hello. I don't quiet understand where to put this code which begins with $. Seriously, I am new to this stuff, can you point me to some resource or just a key words for me to search ? many thanks!
Inside a js function where you have your desired conditions! Check the if...else statement in Javascript!
0

Use http://jsfiddle.net/ to check your code.

Try:

$('#resForm').validate({
  submitHandler: function(form) {
// disable submit button
  $('#btnSubmit').prop('disabled', true);
    $.ajax({
      //..... typical ajax stuff .....
      success: function(data) {
        alert(data);
        $('success').html(data);
        // enable reserve button again
        $('#btnSubmit').prop('disabled', false);
      },
      error: function (jqXHR, textStatus, errorThrown) {
        // console.log(jqXHR);
      }
    });
  }
});

Fiddle with Correction: http://jsfiddle.net/shree/qB9aW/ .JSHint gives you a hint for error.

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.