表单 onsubmit 属性中的 "someFunction()"、"JavaScript:somFuction()" 和 "return someFunct()" 有什么区别?

What is the difference between "someFunction()", "JavaScript:somFuction()", and "return someFunct()" in form onsubmit attribute?

我想知道从表单以这些不同方式调用 JavaScript 函数之间的区别。

方法一

<form onsubmit="someFunction()">

方法二

<form onsubmit="JavaScript:someFunction()">

方法三

<form onsubmit="return someFunction()">

我在任何 HTML5 form 文档中都找不到对 onsubmit 属性的任何提及(只有一些 HTML4 文档的信息)。我不应该使用它吗?在提交按钮上使用 onclick 会更好吗?

onsubmit="someFunction()"

调用函数

onsubmit="JavaScript:someFunction()"

相同,但完全没用 label. The person who wrote it probably saw a javascript: scheme URL in an href attribute (which is a terrible idea unless you are explicitly writing a bookmarklet) and fell into a cargo cult,不理解 on* 属性期望它们的值是 JavaScript 而不是 URL.

onsubmit="return someFunction()"

相同,但提交事件处理函数returns 与函数returns 无关。如果它 returns false 它会阻止表单提交的默认行为。

I couldn't find any mention of the onsubmit attribute in any HTML5 form docs

这是笼统的定义here

Should I not use it?

固有事件属性违反了separation of concerns and have some really while also depending on the functions called from them to be globals的原则。

最好避免使用它们。

现代 JS 将使用 addEventListener and prevent the default behaviour of the form submission with preventDefault

绑定事件处理程序

Would it be better to use onclick on the submit button?

没有。可以通过其他机制触发表单提交,例如在文本输入中按下回车键。虽然这可能会触发提交按钮上的点击事件,但最好说出你的意思。