Raghav Raghav - 1 year ago 116
Javascript Question

How to stop a form from refreshing even after submit / post

I have a form which does a 'post' to java code, on submit.
The requirement is as follows - On submit an alert box should come with 'ok' button where the user will read the info. and then click 'ok'.
This is just a info dialog, and nothing else.
But the alert box should stay there until he clicks 'ok' button.
I have given jquery submit and also tried form submit . The form is getting refreshed and alert box is going out of screen onsuccessful submit , as the entire form is getting posted and refreshes the screen.

Answer Source

You should use the confirmation box, and prevent the default action if it isn't confirmed. See my comments in the code.

$(function() {
  $('form').on('submit', function(e) {
    if( !window.confirm('Do you want to send this form') ) { /* ask for confirmation */
      e.preventDefault(); /* if not confirmed, stop the default action, else send it */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <input type="text" />
  <input type="submit" value="Send" />