单击取消按钮后保持当前页面

时间:2012-03-01 06:28:39

标签: javascript jquery forms jsp alert

我在index.jsp中有一个表单,点击提交后我显示警告“确认提交?”如果单击 ok ,则会显示confirmmsubmit.jsp。我在request.getParameter("textboxname");的confirmmsubmit.jsp中获取文本框名称但问题是如果我点击取消然后确认提交.jsp正在打开,如何在点击取消按钮后留在index.jsp中警惕?

请帮助

的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> 

3 个答案:

答案 0 :(得分:9)

在else部分添加以下行:

return false;

并将您的onclick更改为:

return confirmation();

=== UPDATE ===

由于表单操作中包含confirmsubmit.jsp,因此您不需要window.location

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

另见this example

答案 1 :(得分:2)

 <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>  

答案 2 :(得分:0)

return取消onclick,如果return false;

,请添加!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>