为更多字段设置相同的属性 [英] Setting same properties for more fields

查看:104
本文介绍了为更多字段设置相同的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个或多个文本字段,我想对它们应用相同的属性,避免编写两次或多次相同的代码

I have two or more text fields and I want to apply the same properties to them, avoiding to write two or more times the same code

这不起作用:

$("#form").validate({  
    rules:  
    {
    'name', 'surname':{  
        required: true,  
        minlength: 3,
        maxlength: 50   
        }
    }
});   

有什么想法吗?

推荐答案

像这样尝试

var ruleSet1 = {
        required: true,  
        minlength: 3,
        maxlength: 50
    };

$('#form').validate({
    rules: {
        field_1: ruleSet1,
        field_2: ruleSet1,
        field_3: ruleSet1
    }
});

或像下面这样一个一个地做

or do it one by one like the following,

$("#form").validate({  
    rules:  
    {
    name: { required: true },
    surname:{  
        required: true,  
        minlength: 3,
        maxlength: 50   
        }
    }
});

这篇关于为更多字段设置相同的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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