我在 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/