选中复选框时动态更改jQuery Validate规则

我正在使用jQuery Validation来验证表单,我想要做的就是这样……我想要设置它,如果选中复选框,那么编辑框的验证方式也不同,例如.

如果未选中该复选框,则应如何验证:

weight: { required: true,max: 50 }

如果选中它,应该如何验证它.

weight: { required: true,max: 100 }

有任何想法吗?

最佳答案
只要单击复选框,就可以使用Validate plugin’s built-in rules('add') method动态更改规则.

jQuery的:

$(document).ready(function () {

    // initialize the plugin
    $('#myform').validate({ 
        // other options,rules: {
            // other rules,weight: {
                required: true,max: 50 // initial value on load
            }
        }
    });

    // change the rule on checkBox and update displayed message dynamically
    $('#check').on('change',function () {
        if ($(this).is(':checked')) {
            $('#weight').rules('add',{
                max: 100
            });
        } else {
            $('#weight').rules('add',{
                max: 50
            });
        };
        $('#weight.error').each(function () {
            $(this).valid();
        });
    });

});

HTML:

Box" id="check" />

工作演示:http://jsfiddle.net/3hGxS/

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: <span id=&quot...
jQuery 添加水印 <script src="../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...