使用 onSubmit 调用时,Javascript 函数不起作用
我在 onSubmit 方面遇到问题。 我正在使用 jQTouch 构建一个小型移动网站。 如果用户按 Enter 键,我想使用 onSubmit 调用一个函数,但这不起作用。 我真的不知道问题出在哪里,因为当 onclick 调用该函数时,该函数工作正常。
这是 HTML:
<form action="" method="get" name="form1" onSubmit="calculateValue1();return false">
<ul>
<li>
Input 1 <input id="input1" name="input1" type="number"/>
</li>
<li>
Input 2 <input id="input2" name="input2" type="number"/>
</li>
</ul>
</form>
<a onclick="calculateValue1()">Calculate</a>
这是函数calculateValue1() 的javascript:
function calculateValue1() {
M = window.form1.input1.value;
n = window.form1.input2.value;
if (window.form1.input1.value=="") {
alert("Value missing");
}
}
当使用onclick 单击链接时,该函数起作用。当焦点位于输入字段 1 并按 Enter 键时,没有任何反应。
这可能有什么问题?按 Enter 时没有收到错误消息,我真的不知道 onSubmit 出了什么问题。
I'm having a problem with onSubmit.
I'm building a little mobile website using jQTouch and.
I want to call a function with onSubmit, if the user hits Enter, but that does not work.
I really don't know what's the problem, because the function works perfectly, when it's called by onclick.
So here's the HTML:
<form action="" method="get" name="form1" onSubmit="calculateValue1();return false">
<ul>
<li>
Input 1 <input id="input1" name="input1" type="number"/>
</li>
<li>
Input 2 <input id="input2" name="input2" type="number"/>
</li>
</ul>
</form>
<a onclick="calculateValue1()">Calculate</a>
And here's the javascript for the function calculateValue1():
function calculateValue1() {
M = window.form1.input1.value;
n = window.form1.input2.value;
if (window.form1.input1.value=="") {
alert("Value missing");
}
}
When clicking on the link with onclick, the function works. When the focus is in input field 1 and I hit enter, nothing happens.
What could be the problem with this? I don't get an error message when hitting enter, I really don't know what's the problem with onSubmit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的表单中缺少提交按钮。
You're missing a submit button in your form.
当您通过按“提交”类型的输入或在输入字段中按 Enter 键提交表单时,会触发 onSubmit 事件,但为了使表单在按 Enter 键时提交,您需要有一个适当的提交按钮。那么您将不需要提交锚链接:
The onSubmit event is triggered when you submit a form either via the pressing an input of type "submit" or when you press enter in an input field, but in order for the form to submit on pressing the enter key you need to have a proper submit button. You won't need a anchor link for the submit then: