gpt4 book ai didi

javascript - html 需要不与 onclick 一起工作

转载 作者:太空宇宙 更新时间:2023-11-04 15:21:09 24 4
gpt4 key购买 nike

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

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

我在表单中输入了所需的标签,但这似乎并没有触发它们?这意味着不会阻止提交空输入。

如果我使用普通的提交按钮,它可以正常工作 - onclick 不是一种在 html5 中提交表单的公认方式吗?

最佳答案

问题是 submit() 方法,而不是 onclick 的使用(虽然如果你想使用 JS 而不是 HTML 提交,你无法避免前者).

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

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

http://www.w3.org/TR/html5/forms.html#dom-form-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

关于javascript - html 需要不与 onclick 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9066232/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com