If I click the Submit button once then the AJAX call is executed only once. This is perfect.
The problem is if I don't refresh the page and click the Submit button for the second time, then the AJAX call is executed twice. If I click the button for a third time without refreshing the page, the AJAX call is executed thrice.
What is wrong with my code? I want the AJAX call to be executed only once when I click the Submit button.
<form id="myForm">
<div class="modal-dialog" role="document">
<input type="hidden" name="buttonId" id="buttonId">
<div class="modal-content">
<div class="modal-body">
<div class="input-group">
<span id="commspan" class="input-group-addon">Comment</span>
<input type="text" name="queuqOther" id="queuqOther" class="form-control">
</div>
</div>
<div class="modal-footer">
<button type="button" id="btnPause" class="btn btn-primary">Submit</button>
</div>
</div>
</div>
</form>
$('#btnPause').on('click', function() {
var form = $("#myForm");
form.validate();
if (form.valid()) {
$.ajax({
async: true,
crossDomain: true,
url: "http://localhost/postdata",
xhrFields: {
withCredentials: true
},
success: function(data) {
console.log(data);
}
});
}
});
$('#btnPause').off().on('click'...You keep attaching a new handler every time.off(). Is there any other logic called when the AJAX returns as this code will not cause the behaviour you describe