0

如何隐藏特定字段的 jQuery 验证错误消息?

在我的“联系我们”页面中,我有四个文本框,一个下拉菜单和一个textarea. 我的最后一个文本框根据下拉菜单中选择的值显示或隐藏。所有字段都通过 jQuery Validation 插件进行验证。

现在,当通过选择下拉菜单隐藏最后一个文本框时,我也想隐藏它的 jQuery Validation 错误消息。

我怎样才能做到这一点?

4

1 回答 1

1

您可以通过使用该方法的addremove函数来执行下面的示例.rules()。请参阅: http ://docs.jquery.com/Plugins/Validation/rules#.22add.22rules

$('form').validate({
    // your other rules and options
});

var ruleSet = {
    required: true,
    minlength: 5
}

$('#field').rules("add", ruleSet);

$('button').click(function() {
    display = $('#field').css('display');
    if (display == 'none') {
      $('#field').show().rules("add", ruleSet);  // show the field and add the rule set
    } else {
      $('#field').next('label').remove(); // remove any outstanding message
      $('#field').hide().rules("remove", ruleSet);  // hide the field and remove the rule set
    }
});

工作演示:

http://jsfiddle.net/jqg7s/

于 2013-01-09T01:10:26.003 回答