I have a form that takes a little while for the server to process. I need to ensure that the user waits and does not attempt to resubmit the form by clicking the button agai
Timing approach is wrong - how do you know how long the action will take on client's browser?
$('form').submit(function(){
$(this).find(':submit').attr('disabled','disabled');
});
When form is submitted it will disable all submit buttons inside.
Remember, in Firefox when you disable a button this state will be remembered when you go back in history. To prevent that you have to enable buttons on page load, for example.
Why not just this -- this will submit the form but also disable the submitting button,
$('#myForm').on('submit', function(e) {
var clickedSubmit = $(this).find('input[type=submit]:focus');
$(clickedSubmit).prop('disabled', true);
});
Also, if you're using jQuery Validate, you can put these two lines under if ($('#myForm').valid())
.
...but the form is not submitted with any of the POST data it is supposed to include.
Correct. Disabled form element names/values will not be sent to the server. You should set them as readonly elements.
Also, anchors cannot be disabled like that. You will need to either remove their HREFs (not recommended) or prevent their default behaviour (better way), e.g.:
<script type="text/javascript">
$(document).ready(function(){
$("form#my_form").submit(function(){
$('input').attr('readonly', true);
$('input[type=submit]').attr("disabled", "disabled");
$('a').unbind("click").click(function(e) {
e.preventDefault();
// or return false;
});
});
</script>
Use two submit buttons.
<input id="sub" name="sub" type="submit" value="OK, Save">
<input id="sub2" name="sub2" type="submit" value="Hidden Submit" style="display:none">
And jQuery:
$("#sub").click(function(){
$(this).val("Please wait..");
$(this).attr("disabled","disabled");
$("#sub2").click();
});
I ended up using ideas from this post to come up with a solution that is pretty similar to AtZako's version.
jQuery.fn.preventDoubleSubmission = function() {
var last_clicked, time_since_clicked;
$(this).bind('submit', function(event){
if(last_clicked)
time_since_clicked = event.timeStamp - last_clicked;
last_clicked = event.timeStamp;
if(time_since_clicked < 2000)
return false;
return true;
});
};
Using like this:
$('#my-form').preventDoubleSubmission();
I found that the solutions that didn't include some kind of timeout but just disabled submission or disabled form elements caused problems because once the lock-out is triggered you can't submit again until you refresh the page. That causes some problems for me when doing ajax stuff.
This can probably be prettied up a bit as its not that fancy.
Change submit button:
<input id="submitButtonId" type="submit" value="Delete" />
With normal button:
<input id="submitButtonId" type="button" value="Delete" />
Then use click function:
$("#submitButtonId").click(function () {
$('#submitButtonId').prop('disabled', true);
$('#myForm').submit();
});
And remember re-enable button when is necesary:
$('#submitButtonId').prop('disabled', false);