Javascript:onSubmit函数在函数完成之前提交表单?
我问这个问题是因为我自己完全迷失了,需要一双新的眼睛。
提交连接的 HTML 表单时成功调用以下 JavaScript 函数。该函数启动,并运行前两个 if
语句(如果返回 false
,则停止提交)。
然后,第一个测试alert
“before”出现,然后表单提交,完全错过了函数的其余部分。在测试时,我将最后一行更改为返回 false
,以便无论发生什么情况,函数都应该返回 false
,但表单仍然提交。
function validateForm(form)
{
// declare variables linked to the form
var _isbn = auto.isbn.value;
var _idisplay = auto.isbn.title;
var _iref = "1234567890X";
// call empty string function
if (EmptyString(_isbn,_idisplay)==false) return false;
// call check against reference function
if (AgainstRef(_isbn,_iref,_idisplay)==false) return false;
// call check length function
alert("before");///test alert
////// FORM SUBMITS HERE?!? /////////////
if (AutoLength(_isbn)==false) return false;
alert("after");///test alert
// if all conditions have been met allow the form to be submitted
return true;
}
编辑:这就是 AutoLength
的样子:
function AutoLength(_isbn) {
if (_isbn.length == 13) {
return true; {
else {
if (_isbn.length == 10) {
return true; {
else {
alert("You have not entered a valid ISBN10 or ISBN13. Please correct and try again.");
return false;
}
}
I'm asking this because I am at a complete loss myself and need a fresh pair of eyes.
The following JavaScript function is successfully called on submission of the connected HTML form. The function starts and the first two if
statements run (and halt the submission if they return false
).
Then, the first test alert
"before" appears and then the form submits, completely missing out the rest of the function. While testing I changed the final line to return false
so that whatever happen the function should return false
, but the form still submitted.
function validateForm(form)
{
// declare variables linked to the form
var _isbn = auto.isbn.value;
var _idisplay = auto.isbn.title;
var _iref = "1234567890X";
// call empty string function
if (EmptyString(_isbn,_idisplay)==false) return false;
// call check against reference function
if (AgainstRef(_isbn,_iref,_idisplay)==false) return false;
// call check length function
alert("before");///test alert
////// FORM SUBMITS HERE?!? /////////////
if (AutoLength(_isbn)==false) return false;
alert("after");///test alert
// if all conditions have been met allow the form to be submitted
return true;
}
Edit: this is what AutoLength
looks like:
function AutoLength(_isbn) {
if (_isbn.length == 13) {
return true; {
else {
if (_isbn.length == 10) {
return true; {
else {
alert("You have not entered a valid ISBN10 or ISBN13. Please correct and try again.");
return false;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
AutoLength
实现存在错误。目前,它看起来像这样:看看它如何不关闭所有块?那是因为您在两个地方使用了错误的大括号,并且忘记了关闭该函数。
您可以像这样重写该函数:
如果您一心想要使用
alert
,您可以在validateForm
中执行此操作(尽管我会尝试找到更多用户-显示错误消息的友好方式)。将来,当您尝试调试代码时,可以使用
try
和catch
来“捕获”发生的错误
,例如这:There are errors in your implementation of
AutoLength
. Currently, it looks like this:See how it doesn't close all of its blocks? That's because you've used the wrong brace in two places, and you've forgotten to close the function.
You could rewrite the function like this:
If you're hell-bent on using
alert
, you can do that insidevalidateForm
(although I would try to find a more user-friendly way to show the error message).In the future, when you're trying to debug code, you can use
try
andcatch
to "catch"Errors
as they happen, like this:如果函数的执行因运行时错误而终止,则表单将提交。因此,请检查脚本控制台日志中的错误消息。
If the execution of the function is terminated by a runtime error, the form submits. So check out the script console log for error messages.