为什么JS confirm()获胜't当我在确认对话框中点击“取消”时,取消提交操作

Why JS confirm() won't Cancel the submit action when I hit Cancel in confirm dialog?

本文关键字:取消 对话框 操作 提交 确认 获胜 confirm JS 为什么      更新时间:2024-04-24

我试图找出问题出在哪里,但没有任何线索
通过使用HTML5多提交,我有4个按钮用于一个表单。当我点击DELETE按钮时,会弹出一个对话框,使用属性onclick="confirm('message');。当我点击"取消"时,它应该停止表单提交,关闭对话框并保持在同一页面上,而不采取任何行动,相反,它一直在提交表单。到目前为止,它运行得很好,我找不到问题出在哪里。

<form action="http://google.com/index/25" method="post" accept-charset="utf-8">
    <button class="btn" type="submit" name="formSubmit" value="new">New</button>
    <button class="btn" type="submit" name="formSubmit" value="save">Bulk save</button>
    <button class="btn btn-danger" type="submit" name="formSubmit" value="delete" onclick="confirm('Do you really want to remove a record(s)?.');">Delete permanently</button>
    <button class="btn" type="submit" onclick="confirm('stop or proceed');">submit</button>
</form>

jsbin上有现场演示

有人感染了同样的病毒吗?

在表单而不是按钮上处理它,并让处理程序从确认对话框返回结果:

<form onsubmit="return confirm('stop or proceed');">

您也可以处理每个按钮的事件,但不要忘记返回:

<button onclick="return confirm('blabla');">Button</button>

如果您想使用Jquery,您应该使用以下代码:

<a href="www.blahblah.com" class="classTest">click</a>

JQuery

$(function () {
$(".classTest").click(function (e) {
            return confirm("Submit the Post action?");
 });
}