简体   繁体   English

为什么我的函数在jQuery Validation Plugin中的规则内没有返回false?

[英]Why is my function inside a rule in jQuery Validation Plugin not returning false?

In jQuery Validation Plugin I have a rule for an input field that should be set to false if a hidden input has val equal to 1, and true if 2. 在jQuery Validation Plugin中,我有一个输入字段的规则,如果隐藏的输入的val等于1,则应将其设置为false;如果值为2,则应将其设置为true。

I have made a function inside one of the rules for the input field, where I look for the hidden input val, and returning true or false based on the number. 我在输入字段的规则之一内创建了一个函数,在其中查找隐藏的输入val,并根据数字返回true或false。 However, it is returning true no matter what, even though I can see it should be returning false. 但是,无论如何它都会返回true,即使我可以看到它应该返回false。 See JSfiddle JSfiddle

HTML: HTML:

<form>
  <input type="hidden" name="pick" value="1"/>
  <label>Username:</label>
  <input name="user" id="user" type="text"/>
  <input type="Submit"/>
</form>

jQuery: jQuery的:

$('form').validate({
    rules: {
    user: {
        required: true,
        email: function(){
          if($('input[name="pick"]').val() == 1){
            console.log('Email shold be false');
            return false;
          }else{
            return true;
        }
      }
    }
  }
});

Why is "return false" not working? 为什么“返回假”无效? I can see in the console that "Email should be set to false" 我可以在控制台中看到“电子邮件应设置为false”

Note: This is just an example of what I am trying to achieve. 注意:这只是我要实现的示例。 I am going to have a lot more inputs and rule-changes. 我将有更多的输入和规则更改。 Also, the hidden input field is generated by other measures (Not two buttons like in the example). 另外,隐藏的输入字段是通过其他方式生成的(不是示例中的两个按钮)。 I know I could just make 2 forms, but I rather want to do it with 1, because there is going to be a lot of changes and that would require a lot of forms / double js code. 我知道我只能制作2个表格,但我想用1个表格来做,因为将要进行很多更改,这将需要大量表格/双重js代码。

"Why is my function inside a rule in jQuery Validation Plugin not returning false?" “为什么我的函数在jQuery Validation Plugin中的规则内没有返回false?”

It's not. 不是。 The email rule is seeing your function in place of a boolean and interpreting its mere existence as "not false ", thereby enabling the rule. email规则正在查看您的函数以代替布尔,并将其仅存在解释为“ not false ”,从而启用了该规则。

In order to use a function here, it needs to be inside of the depends callback parameter . 为了在这里使用一个函数,它必须在depends回调参数中

Also, since you need to get a boolean in the first place, there is no need for an if/else statement; 另外,由于首先需要获取布尔值,因此不需要if / else语句; just return the result of your conditional statement. 只需return条件语句的结果即可。

Flip the conditional to a "not equals" ( != ), so the email rule is not enforced when the hidden value is 1 . 将条件条件翻转为“不等于”( != ),因此当隐藏值为1时,不强制执行email规则。

rules: {
    user: {
        required: true,
        email: {
            depends: function(element) {
                return ($('input[name="pick"]').val() != 1);
            }
        }
    }
}

Working DEMO: jsfiddle.net/v4bf1mvj/ 工作演示: jsfiddle.net/v4bf1mvj/

You shouldn't set email to be a function ( email: function(){} ). 您不应该将email设置为函数( email: function(){} )。

You should set the properties of the email object so that the framework can read what it needs to. 您应该设置email对象的属性,以便框架可以读取其需要的内容。

So to place your own function in email to validate (return true or false) you should do so like this: 因此,要将自己的函数放入email进行验证(返回true或false),您应该这样做:

$('form').validate({
            rules: {
                user: {
                    required: true,
                    email: {
                        depends: function () {
                            var checkValue = $('input[name="pick"]').val() == 1;
                            return checkValue;

                            // Or this - depending on your requirements.
                            // return !checkValue;
                        }
                    }
                }
            }
        });

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

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