0

在这里提琴:

http://jsfiddle.net/rBhfx/79/

在下划线模板内的这个 for 循环中: <% for(var item in thing.items) { %>

我想得到 item.name,但<%= item.name %>没有输出任何东西。

如何从每个things.items对象中获取属性?谢谢!

我的 JSON 数据如下所示:

var things = [{
    "name": "Chair",
    "title": "Chairs",
    "items": [{
           "name": "Recliner",
           "title": "Recliner Chair",
           "type": "Chair",
           "quantity": "1"
        }, 
        {
           "name": "Club/Armchair",
           "title": "Club/Armchair",
           "type": "Chair",
           "quantity": 1
        }]
}, 
{
   "name": "Table",
   "title": "Tables",
   "items": [{
           "name": "End Table",
           "title": "End Table",
           "type": "Table",
           "quantity": "1"
        }, 
        {
           "name": "Coffee Table",
           "title": "Coffee Table",
           "type": "Table",
           "quantity": 1
        }]
}];

我的模板如下所示:

<script type="text/html" id='furniture-template'>
        <% _.each(things,function(thing,key,list){ 
            // create more variables
        %>
            <div class="accordion-heading">
                <a class="accordion-toggle" data-toggle="collapse" href="#things-<%= thing.name %>">
                    <%= thing.title %>
                </a>
            </div> <!-- header -->

            <div id="things-<%= thing.name %>" class="accordion-body collapse in">
                <div class="accordion-inner">
                    <% for(var item in thing.items) { %>
                    <div class="item">
                        <a class="item-add" data-type="Chairs" data-name="Recliner"><%= item.name %></a>
                    </div>
                    <% } %>
                </div> <!-- inner -->
            </div> <!-- accordion-body -->

        <% }); %>
</script>
4

2 回答 2

1

您是否尝试过使用:

<% _.each(thing.items, function(item) { %>
    …
<% }); %>

代替:

<% for(var item in thing.items) { %>
    …
<% } %>
于 2013-05-01T23:14:51.700 回答
0

<%= thing.items[item].name %>工作!

http://jsfiddle.net/rBhfx/86/

于 2013-05-01T23:09:43.043 回答