form confirm before submit
I am using a simple form and I want to allow the user to confirm before the form submits... I know this would be easy using jQuery, but I am a bit confused about code...
$(function() {
$(".testform").submit(function() {
$('.submitbtn').text('confirm');
});
});
I know that the above code is not complete and I would like your help to finish it, so it would give an alert (using jQuery alert) that says 'Please confirm if everything is correct' and changes the text on the submit button to "confirm" instead of "submit". If the user clicks confirm again it would submit the form. I hope this makes sense. thanks.
$('#myForm').submit(function() {
var c = confirm("Click OK to continue?");
return c; //you can just return c because it will be true or false
});
sample fiddle: http://jsfiddle.net/z68VD/
html:
<form id="uguu" action="http://google.ca">
<input type="submit" value="text 1" />
</form>
jquery:
$("#uguu").submit(function() {
if ($("input[type='submit']").val() == "text 1") {
alert("Please confirm if everything is correct");
$("input[type='submit']").val("text 2");
return false;
}
});
Here's what I would do to get what you want :
$(document).ready(function() {
$(".testform").click(function(event) {
if( !confirm('Are you sure that you want to submit the form') )
event.preventDefault();
});
});
A slight explanation about how that code works, When the user clicks the button then the confirm dialog is launched, in case the user selects no the default action which was to submit the form is not carried out. Upon confirmation the control is passed to the browser which carries on with submitting the form. We use the standard JavaScript confirm here.
HTML
<input type="submit" id="submit" name="submit" value="save" />
JQUERY
$(document).ready(function() {
$("#submit").click(function(event) {
if( !confirm('Are you sure that you want to submit the form') ){
event.preventDefault();
}
});
});