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

发布于 2024-10-18 17:03:31 字数 802 浏览 2 评论 0原文

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

calcTotal(value){
// sample code
}

我在 Velocity 模板语言(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> ...
   "{" ...
   "}" ...

我做错了什么?

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

calcTotal(value){
// sample code
}

I am using the above function calcTotalScore() in Velocity Template Language(VTL), but it says:

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> ...
   "{" ...
   "}" ...

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巷子口的你 2024-10-25 17:03:31

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

您有两个选择:

  1. 用反斜杠转义美元,例如。 \$
  2. 使用 jQuery 而不是 $

VTL is interpreting jQuery's $ as the start of a variable. It then complains because a variable name cannot start with a dot.

You have two options:

  1. Escape the dollar with a backslash, eg. \$
  2. Use jQuery instead of $
楠木可依 2024-10-25 17:03:31

更新

查看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();

UPDATE

see answer of a'r :D


You're just missing a "function" in front of 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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文