简体   繁体   中英

stay in the current page after clicking cancel button

I have a form in index.jsp and after clicking submit i am showing an alert "confirm submit?" if ok will be clicked then confirmsubmit.jsp will be displayed. I am getting text box name in confirmsubmit.jsp by request.getParameter("textboxname"); But problem is if I click cancel then also confirmsubmit.jsp is opening, how can I stay in index.jsp after clicking cancel button in alert?

Any help please

index.jsp

<form action="confirmsubmit.jsp" method="POST">
<script type="text/javascript">
<!--
function confirmation() {
var answer = confirm("Confirm submit?")
if (answer){

    window.location = "confirmsubmit.jsp";// goes to confirmsubmit.jsp
}
else{
    //should remain in index.jsp but here also confirmsubmit.jsp is opening
}
}
//-->
</script>
<input type="text" name="textboxname"/>
<input type="submit" onclick="confirmation()"/> 
</form> 

Add following line in the else part:

return false;

and change your onclick to:

return confirmation();

=== UPDATE ===

Because you have the confirmsubmit.jsp in the form action, you don't need the window.location :

function confirmation() {
    if (!confirm("Confirm submit?")) {
        return false;
    }
}

Also see this example .

 <form action="confirmsubmit.jsp" method="POST">
    <script type="text/javascript">
    <!--
    function confirmation() {
    var answer = confirm("Confirm submit?")
    if (answer){

        window.location = "confirmsubmit.jsp";// goes to confirmsubmit.jsp
        return true;
    }
    else{
        //should remain in index.jsp but here also confirmsubmit.jsp is opening
    return false;
    }
    }
    //-->
    </script>
    <input type="text" name="textboxname"/>
    <input type="submit" onclick="return confirmation()"/> 

</form>  

Take off return from onclick and add return false; if !answer

<script type="text/javascript">
function confirmation() {
    var answer = confirm("Confirm submit?")
    if (!answer){
        return false;
    }
}
</script>

<form action="confirmsubmit.jsp" method="POST">
    <input type="text" name="textboxname"/>
    <input type="submit" onclick="confirmation()"/> 
</form>  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM