user2452357 user2452357 - 4 months ago 16
jQuery Question

JQuery dialog to pass back true or false to submit function

I am using the submit function in Jquery to do some validation, if the validation is true it submits the form. However, I've added an 'are you sure' dialog. Can I pass back true or false from the dialog which would then submit the form as currently I'm stuck in a loop.

JQuery:

$(function(){
<!-- form validation -->
$("#form1").submit(function() {

//validation stuff

//Disable the submit button
$('#SubmitButton').attr('disabled', false);



// Only if true is returned from dialog??
return true;
}) });


html

<div id="testdialog" title="Please Check">
<p>Are you sure></p>
<div style="height: 30px; width:100px;"onclick="$('#testdialog').dialog('close');return true;">
Continue
</div>
<div style="height: 30px; width: 100px;" onclick="$('#testdialog').dialog('close');return false';">
Redo selection
</div>
</div>

Answer

You would need to set a global/session variable that tells you if the user has already confirmed or not, something like this:

var confirmed = false;
var confirmSubmit = function() {
    $('#testdialog').dialog('close');
    confirmed = true;
    $("#form1").submit();
    return true;
}
$(function(){
     <!-- form validation -->
     $("#form1").submit(function() {     

     //validation stuff

     //Disable the submit button 
     $('#SubmitButton').attr('disabled', false);

     if (!confirmed) {
        // Trigger the notification
     }


     // Only if true is returned from dialog??
    return confirmed;
}) });

and the html:

<div id="testdialog" title="Please Check">
    <p>Are you sure></p>                            
    <div style="height: 30px; width:100px;"onclick="confirmSubmit">
       Continue
    </div>
    <div style="height: 30px; width: 100px;" onclick="$('#testdialog').dialog('close');return false';">
       Redo selection
    </div>
</div>