html不需要使用onclick [英] html required not working with onclick

查看:117
本文介绍了html不需要使用onclick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下用于在我的网站上提交表单:

I have the following which I use for submitting forms on my site:

<a onclick="document.forms['REGform'].submit(); return false;" class="button" href="javascript:;">Register</a>

我在表格中输入了所需的标签,但这似乎没有解雇它们?含义不会停止提交的空输入。

I have inputs within the form with the required tag but this does not seem to fire them? Meaning doesn't stop empty inputs being submitted.

如果我使用正常的提交按钮,它可以正常工作 - 点击不是公认的方式提交表单html5?

If I use a normal submit button it works fine by the way - is onclick not a recognised way to submit a form within html5?

推荐答案

问题是 submit()方法,而不是使用 onclick (如果您想使用JS而不是HTML提交,则无法避免使用前者)。

The problem is the submit() method, not the use of onclick (although you can't avoid the former if you want to submit using JS instead of HTML).

使用JS提交会导致跳过表单的验证步骤(尽管您可以在JS中重新实现它们(使用 checkValidity() )。

Submitting using JS causes the form's validation steps to be skipped (although you can re-implement them in JS (with checkValidity()).


调用submit()方法时,必须从表单元素本身提交表单元素,并设置脚本提交标志。

The submit() method, when invoked, must submit the form element from the form element itself, with the scripted-submit flag set.

- http://www.w3。组织/ TR / HTML5 / forms.html #dom-form-submit


如果未设置scripted-submit标志,且提交者元素的无验证状态是假的,然后以交互方式验证表单的约束并检查结果:如果结果是否定的(约束验证得出结论有无效字段并且可能告知用户这个),则中止这些步骤。

If the scripted-submit flag is not set, and the submitter element's no-validate state is false, then interactively validate the constraints of form and examine the result: if the result is negative (the constraint validation concluded that there were invalid fields and probably informed the user of this) then abort these steps.

- http://www.w3.org/TR/html5/association-of-controls-and-forms.html#form-submission-algorithm

这篇关于html不需要使用onclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆