jQueryValidate-必填字段-取决于无效 [英] jQueryValidate - Required field - depends doesn't work

查看:498
本文介绍了jQueryValidate-必填字段-取决于无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用jQueryValidate,我试图仅在另一个特定字段为空时才使该字段成为必需字段.因此,在Internet上搜索之后,我这样做了:

With jQueryValidate, I'm trying to make a field required only if another specific field is empty. So, after searching on the Internet, I did this :

rules: {
     marque: {
          required: {
               depends: function() {
                    if (($('#add_marque').val() == "Ajouter une marque" && $('#marque').val() == -1) || ($('#add_marque').val() == "" && $('#marque').val() == -1)) {
                         return true;
                     }
                     else {
                         return false;
                     }
               }
          }
     }
}

不幸的是,它不起作用.但是,如果我这样做:

Unfortunately, it doesn't work. But, if I do this :

depends: function() {
     if (($('#add_marque').val() == "Ajouter une marque" && $('#marque').val() == -1) || ($('#add_marque').val() == "" && $('#marque').val() == -1)) {
          alert("Here!");
          return true;
      }
      else {
           return false;
      }
}

出现警报框.因此,我的测试似乎有效. 谁能告诉我我做错了什么? 我正在将jQuery 1.8.3和qTip2与jQueryValidate一起使用. 非常感谢你!

The alert box appears. So, my test seems to work. Can anyone tell me what I did wrong? I'm using jQuery 1.8.3 and qTip2 with jQueryValidate. Thank you very much!

推荐答案

您提供的语法和插件使用正确.您需要进行故障排除,因此请首先尝试

Your syntax and use of the plugin is correct from what you have given. You need to troubleshoot, so first try this

marque: {
    required: true;
}

简单.是否出现错误信息?如果是,那就试试

simple. Does the error message appear? If yes then try this

marque: {
    required: {
        depends: function () { return true; /* or false */ }
    }
}

这篇关于jQueryValidate-必填字段-取决于无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆