我正在使用
JQuery Validation.现在我希望仅在满足某些条件时调用我的规则,即我想要$(“#AppSelectField”).is(‘:hidden’)返回false,然后我才调用规则.
我的规则如下:
$(function() { $("#RequestLock").validate({ rules: { FloorNum:{ required: true,digits: true },},message: { FloorNum: "The floor number must be integer",}); });
解决方法
这应该这样做:
$(function() { var checkIfFieldVisible = function() { return !$("#AppSelectField").is(':hidden'); }; $("#RequestLock").validate({ rules: { FloorNum: { required: { depends: checkIfFieldVisible },digits: { depends: checkIfFieldVisible } },// <-- EXTRA COMMA },message: { FloorNum: "The floor number must be integer",// <-- EXTRA COMMA }); });