js中eval的一个使用示例

来源:互联网 发布:淘宝宝贝编辑教程 编辑:程序博客网 时间:2024/05/01 00:34

1、使用jquery的tmpl模板插件时,如下代码是展示一个表格:

<script type="text/x-jquery-tmpl" id="groupInfo">
<tr>
<td>{{= AgentName}}</td>
<td>{{= isCombine}}</td>
<td>{{= isRebate}}</td>
</tr>
</script>

var  jdata=[{AgentName:"rhythmk1", isCombine: 12 , isRebate: 12},
                    {AgentName:"rhythmk2", isCombine: 12 , isRebate: 12},
                    {AgentName:"rhythmk3", isCombine: 15 , isRebate: 12},
                    {AgentName:"rhythmk4", isCombine: 16 , isRebate: 12}];

$("#groupInfo").tmpl(eval(groups)).appendTo("#groupInfoDiv");



2、当时用后台数据时,alert的数据如下

var groups = '<s:property value="#request.groups" escape="false"/>';

alert(groups);

$("#groupInfo").tmpl(groups).appendTo("#groupInfoDiv");


但是 页面是始终展现不出来数据,后来终于想到了eval,即:把字符串执行成一个数组。于是代码改成了

$("#groupInfo").tmpl(eval(groups)).appendTo("#groupInfoDiv");

这一下就好使了。

0 0