简体   繁体   English

为什么当我在“确认”对话框中单击“取消”时,JS Confirm()不会取消提交动作?

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

I tried to find out where is the problem, but no clue. 我试图找出问题出在哪里,但是没有任何线索。
I have 4 buttons for one form by using HTML5 multi submit. 通过使用HTML5多提交,我对一种表单有4个按钮。 When I click on DELETE button, a dialog pops out, by using an attribute onclick="confirm('message'); . When I hit Cancel, it should stop form submission, close the dialog and stay on the same page without any actions, instead it keeps submitting the form. It works perfectly until now, and I can't find out where is the problem. 当我单击DELETE按钮时,使用属性onclick="confirm('message');弹出一个对话框。当我单击Cancel时,它将停止表单提交,关闭对话框并保持在同一页面上,而无需执行任何操作,而是一直提交表单,直到现在为止都可以正常运行,而我找不到问题出在哪里。

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

And a live DEMO IS HERE on jsbin jsbin上有一个现场演示

Anyone got the same bug ? 任何人都有相同的错误吗?

Handle it on the form instead of the button, and have the handler return the outcome from the confirm dialog: 在表单而不是按钮上处理它,并让处理程序从确认对话框返回结果:

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

You can also handle the events of each button, but don't forget to return: 您还可以处理每个按钮的事件,但不要忘记返回:

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

If you wanna use Jquery, you should use this code: 如果要使用Jquery,则应使用以下代码:

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

JQuery JQuery的

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM