简体   繁体   English

为什么在返回false后仍仍提交此表单?

[英]why is this form still submitting after return false?

I am planning on showing/hiding divs to validate a form. 我正计划显示/隐藏div以验证表单。 But the divs only show a split second, and then the form gets submitted... 但是div只显示一瞬间,然后提交表单...

Any ideas why? 有什么想法吗?

here is the code: 这是代码:

function validateForm() {
var name = nameEmpty(document.getElementById("annonsera_name"));
if (nameEmpty(name)){return false;}
return false;
     }
function nameEmpty(fld){
    if (fld.value==0) {
        document.getElementById("annonsera_nameempty_error").style.display='block'; 
        document.getElementById("annonsera_namenotvalid_error").style.display='none';
        document.getElementById("annonsera_name").focus();
        return false;
    }
    else if (fld.value.match(alphaExp)) {
        document.getElementById("annonsera_namenotvalid_error").style.display='block';  
        document.getElementById("annonsera_name").focus();
        document.getElementById("annonsera_nameempty_error").style.display='none';  
        return false;
    }

         document.getElementById("annonsera_nameempty_error").style.display='none'; 
        document.getElementById("annonsera_namenotvalid_error").style.display='none';
    return false;

            }

and here is the form: 形式如下:

<form name="annonsera" id="annonsera" method="post" enctype="multipart/form-data" onSubmit="return validateForm();">

It's probably generating an error and therefore never returning anything. 它可能会产生错误,因此从不返回任何内容。 Assuming you are using Firefox, have you checked the javascript console? 假设您使用的是Firefox,是否检查了JavaScript控制台?

I see what looks like a problem here, where you call nameEmpty() twice - once with a field, and the other time with a (presumably?) boolean value: 我在这里看到了一个问题,您两次调用nameEmpty()-一次使用一个字段,另一次使用一个(大概是)布尔值:

var name = nameEmpty(document.getElementById("annonsera_name")); // Returns boolean
if (nameEmpty(name)){return false;} // Tries to pass boolean back into nameEmpty?

Maybe a bit offtopic, but here goes... 也许有点题外话,但是这里...

You should really consider JQuery Validation , it's nice, clean and easy to implement. 您应该真正考虑JQuery Validation ,它很好,干净并且易于实现。 I've done it a few times, and never looked back since. 我已经做过几次,此后再也没有回头。 It sure beats the manual validation method like the one you use :) 它肯定比您使用的手动验证方法好:)

Check out the example , there really is not that much code to write, to get validation going. 查看示例 ,确实没有太多的代码可编写以进行验证。

In your example that would be (not tested): 在您的示例中(未经测试):

<script>
  $(document).ready(function(){
    $("#annonsera").validate({
      rules: {
        annonsera_name: {
          required: true,
          minlength: 1
        }
      },
      messages: {
        annonsera_name: {
          required: "You need to write your name",
          minlength: jQuery.format("At least {0} characters required!")
        }
      }
    })
  });
</script>

There are also a lot of ways of customization through dhtml and css, check out the options . 还有很多通过dhtml和CSS进行自定义的方法,请查看选项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM