简体   繁体   English

我的Onclick处理程序返回false,但是我的表单仍然提交

[英]My Onclick handler returns false, but my form is still submitted

I have a form with a submit button, and a validator attached to it via onclick=.... When I click on the submit button, the validator is executed and returns false. 我有一个带有提交按钮的表单,并通过onclick = ....附加了一个验证器。当我单击提交按钮时,验证器将执行并返回false。 Still, the form is submitted. 仍然,表单已提交。 How can this be? 怎么会这样?

To narrow the problem further, I even changed the code temporarily to: 为了进一步缩小问题的范围,我什至暂时将代码更改为:

<form id="contactform" method="post" action="cgi-bin/process_contact_form.rb">
    ....
    <input type="submit" value="Abschicken" onclick="false && k_validate('de')" />
</form>

This would just return false and bypass my validator. 这只会返回false并绕过我的验证器。 Indeed, even in this version, the form gets submitted. 实际上,即使在此版本中,也要提交表单。

Note that I am aware that there are means to get a form submitted with bypassing the validator. 请注意,我知道有一些方法可以绕过验证程序来提交表单。 I am here only interested in the "normal" case, ie when a user explicitly clicks the button. 我在这里仅对“正常”情况感兴趣,即当用户明确单击按钮时。

I have tested this on Mac OSX 10.6.8, using the following browsers: 我已经使用以下浏览器在Mac OSX 10.6.8上进行了测试:

SeaMonkey 2.0.14 Safari 5.1.10 Chrome 36.0 SeaMonkey 2.0.14 Safari 5.1.10 Chrome 36.0

JavaScript is, of course, enabled. 当然,启用了JavaScript。

The test page with the aforementioned "false" shortcut can be seen here: 您可以在此处看到带有上述“ false”快捷方式的测试页:

http://www.tsukaeru-doitsugo.de/testing.html http://www.tsukaeru-doitsugo.de/testing.html

It has two buttons. 它有两个按钮。 The first button (labelled "Abschicken") is the one within the form. 表单中的第一个按钮(标记为“ Abschicken”)。 Its onclick handler returns false, but the form is still submitted. 它的onclick处理程序返回false,但仍提交表单。

您缺少退货单:

<input type="submit" value="Abschicken" onclick="return k_validate('de')" />

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

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