javascript - 点击取消按钮后停留在当前页面

标签 javascript jquery forms jsp alert

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

请帮忙

索引.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> 

最佳答案

在else部分添加如下行:

return false;

并将您的 onclick 更改为:

return confirmation();

===更新===

因为表单操作中有 confirmsubmit.jsp,所以不需要 window.location:

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

另见 this example .

关于javascript - 点击取消按钮后停留在当前页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9511626/

相关文章:

javascript - 尝试使用两个表格和三个div创建固定的标题效果

javascript - 我需要 JavaScript 表单验证吗?

php - 表单输入图像换行

php - jQuery依赖字段(重置值),添加到MySQL同名属性字段

javascript - Gwt 使用 JSNI 操作 window.location.hash

javascript - 需要帮助创建一个按钮来激活 javascript 函数

javascript - 提交时更改表单值

javascript - ul li 子菜单使用 jquery 显示 onclick

c# - 在 bootstrap 模态主体中动态添加的 asp.net 按钮/链接按钮 webcontrol 不回发

javascript - 如何判断一个字符串是否是一个数组?