最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - stay in the current page after clicking cancel button - Stack Overflow

programmeradmin1浏览0评论

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> 

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> 
Share Improve this question edited Mar 1, 2012 at 10:00 Niks 4,8324 gold badges38 silver badges55 bronze badges asked Mar 1, 2012 at 6:28 TomTom 7717 gold badges22 silver badges42 bronze badges 1
  • Add return with confirmation() method i.e <input type="submit" onclick="return confirmation()"/> – Amit Soni Commented Mar 1, 2012 at 6:35
Add a comment  | 

3 Answers 3

Reset to default 13

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>  
发布评论

评论列表(0)

  1. 暂无评论