1

First sorry for my bad English.

I would like to show a confirmation layer (id="confirmwin") before submitting a form (id="form"). There is another button (id="yes") to submit the form.

I tried this:

Layer:

<div id="confirmwin" style="display:none">
Are you sure to submit?<br>
<a href="#" id="yes">Yes</a> <a href="#" onclick="closeconfirmwin()">No</a>
</div>

JavaScript:

$(document).ready(function(){
  $("#yes").click( function() {
    $("#form").off("submit").submit();
  });
  $("#form").on("submit", function() {
    $('#confirmwin').show();
    return false;
  });
});

Sometimes (not always) it looks like it's in an endless loop.

Perhaps the #yes click event's off event goes wrong.

1
  • What did you try so far to get this working? Commented Jan 23, 2013 at 15:15

4 Answers 4

1

Have you tried removing the .submit() while turning it off?

$("#form").off("submit").submit();

shouldn't it be..

$("#form").off("submit")  //if

and only if confirmed proceed to do..

$("#form").submit(); //then you can /off() but I don't see the need to
Sign up to request clarification or add additional context in comments.

Comments

1

One solution would be to pass extra parameters while triggering the submit event. You can read more about event data in the jQuery API here.

$("#yes").click( function() {
    $("#form").trigger("submit", [true]);
});

$("#form").on("submit", function(e, blnConfirmed) {
    // If it's not confirmed yet, show the overlay.
    if (!blnConfirmed) {
        $('#confirmwin').show();
        return false;
    } // else it actually IS confirmed, do a real submit
});

You should test this code first. It's just an example.

Comments

1

It is easy:

Just add an global var:

var ignoreConfirm = false;

so it will look like:

$("#yes").click( function() {
  ignoreConfirm = true;
  $("#form").off("submit").submit();
});

$("#form").on("submit", function() {
  if(!ignoreConfirm){
   $('#confirmwin').show();
   return false;
  }
});

Comments

1

simples as this

<form id="xpto" name="" action="" ... onsubmit="return callJavascriptFunctionConfirm();">

//bla bla bla here

</form>

function callJavascriptFunctionConfirm(){
   //do the logic here
   if (yes) 
      return true; 
   else
      return false;
}

}

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.