0

I have following code. where i'm using the Jquery template but it is not accepting - (dash) in class-no. I'm getting an error Uncaught ReferenceError: no is not defined. Is there any solution for this issue with jquery template.

var json = [{"class-no":"12","marks":"500","marks1":"200","marks2":"300"},{"class-no":"11","marks":"200","marks1":"300","marks2":"400"}]

$.template('kList','<tr title="${class-no}"><td>${marks}</td><td>${marks1}</td><td>${marks2}</td></tr>');  

for(var i=0; i<json.length; i++){   
    $.tmpl('kList',json[i]).appendTo("#table1")
}
4

1 回答 1

0

只需在模板调用时在选项参数中提供一个自定义函数,并将模板中的函数用作,

classNo是一个自定义函数,它使用字符串表示法访问对象属性。

$.template('kList','<tr title="${$item.classNo()}"><td>${marks}</td><td>${marks1}</td><td>${marks2}</td></tr>');  

for(var i=0; i<json.length; i++){   
    $.tmpl('kList',json[i], { 
        classNo : function(){ 
            return this.data["class-no"];
        }
    }).appendTo("#table1");
}

希望这可以帮助。

于 2013-04-20T10:05:15.747 回答