尾随逗号问题,javascript

时间:2009-11-30 13:27:49

标签: javascript jquery

我正在努力让我的jQuery在IE7中工作,当我发现以下内容时:

$(".regflow").validate({
    errorLabelContainer: $("#error-message"),
    rules: {
        txtTextOnly: {required: true,textOnly: true},
        txtNumbersOnly: {required: true,numbersOnly: true},
        txtPhoneOnly: {required: true,phoneOnly: true},
        txtAreaCodeOnly: {required: true,numbersOnly: true},
        txtSimCodeOnly: {required: true,simCodeOnly: true},
        txtCprOnly1: {required: true,numbersOnly: true},
        txtCprOnly2: {required: true,cprOnly1: true},
        txtMailOnly: {required: true,mailOnly: true}
    },

    messages: {
        txtTextOnly: {required: "Dette felt skal udfyldes"},
        txtNumbersOnly: {required: "Dette felt skal udfyldes"},
        txtPhoneOnly: {required: "Dette felt skal udfyldes"},
        txtAreaCodeOnly: {required: "Dette felt skal udfyldes"},
        txtSimCodeOnly: {required: "Dette felt skal udfyldes"},
        txtCprOnly1: {required: "Dette felt skal udfyldes"},
        txtCprOnly2: {required: "Dette felt skal udfyldes"},
        txtMailOnly: {required: "Dette felt skal udfyldes"}
    },
});

...我从Lint收到此错误:“警告:尾随逗号在ECMA-262对象初始值设定项中不合法”

有谁知道这意味着什么,我该如何解决?

5 个答案:

答案 0 :(得分:11)

删除尾随逗号 - 以下部分末尾的逗号:

 messages: {
        txtTextOnly: {required: "Dette felt skal udfyldes"},
        txtNumbersOnly: {required: "Dette felt skal udfyldes"},
        txtPhoneOnly: {required: "Dette felt skal udfyldes"},
        txtAreaCodeOnly: {required: "Dette felt skal udfyldes"},
        txtSimCodeOnly: {required: "Dette felt skal udfyldes"},
        txtCprOnly1: {required: "Dette felt skal udfyldes"},
        txtCprOnly2: {required: "Dette felt skal udfyldes"},
        txtMailOnly: {required: "Dette felt skal udfyldes"}
    },

在创建对象{}或数组[]时,您可以用逗号分隔各个元素。但是在[a, b, c,]之类的最后一项之后还有一个额外的逗号 - 根据ECMA-262不允许这样做。

答案 1 :(得分:6)

底部附近的额外逗号:

$(".regflow").validate({
    errorLabelContainer: $("#error-message"),
    rules: {
        txtTextOnly: {required: true,textOnly: true},
        txtNumbersOnly: {required: true,numbersOnly: true},
        txtPhoneOnly: {required: true,phoneOnly: true},
        txtAreaCodeOnly: {required: true,numbersOnly: true},
        txtSimCodeOnly: {required: true,simCodeOnly: true},
        txtCprOnly1: {required: true,numbersOnly: true},
        txtCprOnly2: {required: true,cprOnly1: true},
        txtMailOnly: {required: true,mailOnly: true}
    },

    messages: {
        txtTextOnly: {required: "Dette felt skal udfyldes"},
        txtNumbersOnly: {required: "Dette felt skal udfyldes"},
        txtPhoneOnly: {required: "Dette felt skal udfyldes"},
        txtAreaCodeOnly: {required: "Dette felt skal udfyldes"},
        txtSimCodeOnly: {required: "Dette felt skal udfyldes"},
        txtCprOnly1: {required: "Dette felt skal udfyldes"},
        txtCprOnly2: {required: "Dette felt skal udfyldes"},
        txtMailOnly: {required: "Dette felt skal udfyldes"}
    }, //<--- this one!!!!
});

答案 2 :(得分:3)

不是在谈论:

    }, // This comma here?
}); 

在倒数第二行?

答案 3 :(得分:2)

从最后一行删除最后一个逗号:

$(".regflow").validate({
    errorLabelContainer: $("#error-message"),
    rules: {
        txtTextOnly: {required: true,textOnly: true},
        txtNumbersOnly: {required: true,numbersOnly: true},
        txtPhoneOnly: {required: true,phoneOnly: true},
        txtAreaCodeOnly: {required: true,numbersOnly: true},
        txtSimCodeOnly: {required: true,simCodeOnly: true},
        txtCprOnly1: {required: true,numbersOnly: true},
        txtCprOnly2: {required: true,cprOnly1: true},
        txtMailOnly: {required: true,mailOnly: true}
    },

    messages: {
        txtTextOnly: {required: "Dette felt skal udfyldes"},
        txtNumbersOnly: {required: "Dette felt skal udfyldes"},
        txtPhoneOnly: {required: "Dette felt skal udfyldes"},
        txtAreaCodeOnly: {required: "Dette felt skal udfyldes"},
        txtSimCodeOnly: {required: "Dette felt skal udfyldes"},
        txtCprOnly1: {required: "Dette felt skal udfyldes"},
        txtCprOnly2: {required: "Dette felt skal udfyldes"},
        txtMailOnly: {required: "Dette felt skal udfyldes"}
    }
});

答案 4 :(得分:0)

我知道这是前一段时间被问过的,但这可能会有所帮助。如果您通过JSLint运行JavaScript,它会告诉您是否有任何“额外逗号”。