【问题标题】:How to iterate over an array of objects with jquery templates each command如何使用每个命令使用 jquery 模板迭代对象数组
【发布时间】:2012-04-15 12:04:42
【问题描述】:

在 Javascript 中,我有一个 User 对象,其中包含一组 department 对象,如下所示:

user = 
{
   departments: [
        {id: 1, name: 'Department 1'},
        {id: 2, name: 'Department 2'},
        {id: 3, name: 'Department 3'}
   ]
}

使用 jquery 模板(版本 1.4.2)我想像这样呈现一个<select> 标签:

<select name="departmentId" id="department">
    {{each departments}}
        <option value="{{= id }}">{{= name }}</option>
    {{/each}}   
</select>

这不会在选项元素中产生任何值。如何使用模板访问每个部门对象的 id 和 name 属性。

【问题讨论】:

  • “不起作用”从不足以说明问题。
  • 修正了问题,因此它不再说“不起作用”,而是解释了代码不会在选项元素中产生值

标签: jquery-templates


【解决方案1】:

像这样使用它

<select name="departmentId" id="department">
{{each departments}}
    <option value="${idField}">${textField}</option>
{{/each}}</select>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-21
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    • 2012-08-10
    • 2021-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多