使用对象节点作为变量

时间:2015-01-15 11:52:28

标签: javascript jquery object jquery-validate

如何将对象节点用作变量,如下面的newEndEventDate

var val = $("form").validate({
    rules: {
        newEventDate: { required: true, regex: /^(19|20)\d\d-?(0[1-9]|1[012])-?(0[1-9]|[12][0-9]|3[01])$/ },
        newEndEventDate: { required: true, regex: /^(19|20)\d\d-?(0[1-9]|1[012])-?(0[1-9]|[12][0-9]|3[01])$/, greaterThan: newEventDate },                       
        newEventText: { required: true, maxlength: 200 },
    },
    messages: {
        ...
    }
}).form();

newEventDate内使用newEndEventDate会出现错误'newEventDate' is undefined。但是如果我注释掉newEndEventDate:行,那么脚本运行正常。这只发生在IE10和IE的所有其他版本中。适用于Chrome和Firefox

1 个答案:

答案 0 :(得分:1)

var rules = {
        newEventDate: { required: true, regex: /^(19|20)\d\d-?(0[1-9]|1[012])-?(0[1-9]|[12][0-9]|3[01])$/ },
        newEndEventDate: { required: true, regex: /^(19|20)\d\d-?(0[1-9]|1[012])-?(0[1-9]|[12][0-9]|3[01])$/ },                       
        newEventText: { required: true, maxlength: 200 },
    };
rules.newEndEventDate.greaterThan = rules.newEventDate;
var val = $("form").validate({
    rules: rules,
    messages: {
        ...
    }
}).form();