jquery 验证 onclick

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9129029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:22:48  来源:igfitidea点击:

jquery validation onclick

jqueryjquery-validate

提问by jtheman

I have an issue with the jquery validate plugin that doesn't make sense to me. Please can anyone see where my mistake is?

我对 jquery 验证插件有一个对我没有意义的问题。请问谁能看到我的错误在哪里?

Here is my HTML:

这是我的 HTML:

<form id="form">
   <input type="text" name="name" class="required" />
   <input type="text" name="email" class="required email" />
</form>
<a id="link">Save</a>

Here is my JS

这是我的 JS

<script src="jquery 1.7.1"></script>
<script src="jquery.validate.1.9"></script>
<script>
    $('#link').click(function()
    {
        $('#form').validate();
        if ($('#form').valid()) // check if form is valid
        {
            // do some stuff
        }
        else 
        {
            // just show validation errors, dont post
        }
    });

</script>

The form never gets validated or at least the .valid()function always returns true but I can't see why? I used the validate plugin for years but not in this context.

表单永远不会得到验证,或者至少.valid()函数总是返回 true 但我不明白为什么?我使用验证插件多年,但不是在这种情况下。

采纳答案by jtheman

Solved it for now. Now the validator does what I want. JS below, same HTML:

暂时解决了。现在验证器做我想要的。下面的 JS,相同的 HTML:

$(function() 
{ 
   $('#link').click(function() 
   { 
      if ($("input[name=name]").valid() && $("input[name=email]").valid()) 
      { 
         // do some stuff 
      } 
      else 
      { 
         // just show validation errors, dont post 
      } 
   }); 
});

回答by Enrique

If you have a link and need to validate a form. Also this function goes to next tab in UI Tabs validating each tab with a form in each tab.

如果您有链接并需要验证表单。此功能也转到 UI 选项卡中的下一个选项卡,验证每个选项卡中的表单。

$('.next-tab').click(function() { 
    //if form is valid
    if ($("#frmSignup").valid()) {
        //activate next tab
        $('#signuptabs').tabs('enable', $(this).attr("rel"));
        //switch to next tab
        $tabs.tabs('select', $(this).attr("rel"));
        return false;
    }
});

回答by gdoron is supporting Monica

You are trying to attach an event to an element which isn't rendered yet

您正在尝试将事件附加到尚未呈现的元素

you have to use the onDOM ready

你必须准备好使用 onDOM

$(function() {
     $('#link').click(function(){
        $('#form').validate();
        if ($('#form').valid()) // check if form is valid
        {
            // do some stuff
        }
        else 
        {
            // just show validation errors, dont post
        }
    });
});

$(functionHandler)== $(document).ready(functionHandler);

$(functionHandler)== $(document).ready(functionHandler);

Rule No.1: Always think, is the DOM must be rendered before that peice of code that you write, It's the most common mistake on this site...

规则 1:总是认为 DOM 是否必须在您编写的代码之前呈现,这是本网站上最常见的错误...

This tip and many more you can read on the jquerytag on this site, read it

您可以jquery在本网站的标签上阅读此提示以及更多内容,请阅读

回答by Peter

You need to put your validation in an "on document ready"

您需要将验证放在“准备好文档”中

$(document).ready(function(){
    $('#form').validate();
});