-1

I have a JSP page where I have put a JS code for confirm() method. I am calling this from the form "onsubmit". The dialog box has 2 buttons "OK' and 'cancel'. On clicking "OK" the form should be submitted which is happening but on click of 'cancel' button I need to redirect the page to some other page. Below is the JS code.

function showDialog() {
    if(confirm("Press OK to confirm")) {
     console.log("Clicked OK, confirmation.");} else {
console.log("Clicked Cancel, confirmation.");      

}

Below is the form tag where I am calling JS function:

<form:form  name="login" id="login" action="<%=loginPass%>" method="POST" autocomplete="off" onsubmit= "showDialog()">
31piy
  • 23,323
  • 6
  • 47
  • 67

1 Answers1

0

You can prevent the default onsubmit functionality if you ´return false;´ there. Then you pass the form to your function and call ´.submit()´ there or redirect the user using ´location.href´.

 function showDialog(form) { if(confirm("Press OK to confirm")) { console.log("Clicked OK, confirmation.");
form.submit();
} else { console.log("Clicked Cancel, confirmation."); 
location.href = "https://whereyouwantto.go";
}



<form:form name="login" id="login" action="<%=loginPass%>" method="POST" autocomplete="off" onsubmit= "showDialog(this);return false;">
Sebastian Speitel
  • 7,166
  • 2
  • 19
  • 38