如何在jQuery中为数组使用$ .each方法?

时间:2011-02-22 13:39:14

标签: jquery vtl

function calcTotalScore(){
    var arr = ["A","B","C","D","E","F"];
    $.each(arr, function(n,val){
        calcTotal(val);
    });
}

calcTotal(value){
// sample code
}

我在速度模板语言(VTL)中使用上述函数calcTotalScore(),但它说:

Encountered "," Was expecting one of:
   <EOF> 
    "(" ...
   <RPAREN> ...
   <ESCAPE_DIRECTIVE> ...
   <SET_DIRECTIVE> ...
   "##" ...
   "\\\\" ...
   "\\" ...
   <TEXT> ...
   "*#" ...
   "*#" ...
   <STRING_LITERAL> ...
   <IF_DIRECTIVE> ...
   <STOP_DIRECTIVE> ...
   <INTEGER_LITERAL> ...
   <FLOATING_POINT_LITERAL> ...
   <WORD> ...
   <BRACKETED_WORD> ...
   <IDENTIFIER> ...
   <DOT> ...
   "{" ...
   "}" ...

我做错了什么?

2 个答案:

答案 0 :(得分:4)

VTL将jQuery的$解释为变量的开头。然后它抱怨,因为变量名称不能以点开头。

您有两种选择:

  1. 以反斜杠逃避美元,例如。 \$
  2. 使用jQuery代替$

答案 1 :(得分:1)

<强>更新

见a'r的回答:D


你只是在calcTotal前面缺少一个“函数”;)

http://fiddle.jshell.net/gabel/7SvUY/1/

function calcTotalScore(){
    var arr = Array("A","B","C","D","E","F");
    $.each(arr, function(n,val){
        calcTotal(val);
    });
}

function calcTotal(value){
// sample code
    alert(value);
}

calcTotalScore();