jquery validate – 如果隐藏字段有值,则有效

我需要根据隐藏字段的值validation字段。 我试过这个自定义规则:

jQuery.validator.addMethod("cityselected", function() { if ($('#featureID').val() == "") return false; else return true; }); cityselect: { required: true, cityselected: true }, 

featureID是一个隐藏的输入,我需要检查它有一个值。 这似乎不起作用。 有任何想法吗?

将“必需”类添加到隐藏输入。

如果您的插件版本大于1.9,则必须在validation过程中明确添加hidden,就像初始化插件时一样:

 $("#form").validate({ // Do not ignore hidden fields ignore: [] });