2013-02-18 80 views
1

我有这样的一个模板:迭代通过与underscore.js值

template: _.template('<% if (inputType == "select") {%><select id="<%= id %>" class="<%= contentClass %>" name="<%= name %>">....options should go here! </select><%}%></p>'), 

在我的模型,其中一个属性是一个数组。想象一下,我有看起来像这样的工作对象:

"contentType":"input", 
"contentClass":"createProject_cat", 
"placeholder":"Project Category", 
"name":"createProject_cat", 
"inputType":"select", 
"id":"3", 
"value":["1","2","3"] 

在这个例子中,我期待在<option>标签从value属性包裹1, 2 and 3,然后输出它们从上面的模板两个<select>标记之间。

我想用option标记包装子数组中的每个值,并输出到上面的模板中。是否有一种简单的方法来遍历这些值,从模板内打印并输出它们?

回答

4

你可以做一样的,如果条件:

<% for(var i=0; i<value.length; i++) { %> 
    <option value="<%= value[i] %>"> 
<% } %>