Javascript 填充隐藏的表单字段

Javascript 填充隐藏的表单字段,javascript,jquery,backbone.js,handlebars.js,mustache,Javascript,Jquery,Backbone.js,Handlebars.js,Mustache,应用程序上的GET操作返回数组中的数据: Value":[{"Id":"6b7","Notes":"testing","CreatedBy":"User1"},{"Id":"6b7","Notes":"Testing 1","CreatedBy":"User2"}] 我使用上述方法填充模板: <div class="create-note"> <form> **<input type="hidden" id="Id" name="Id" v

应用程序上的GET操作返回数组中的数据:

Value":[{"Id":"6b7","Notes":"testing","CreatedBy":"User1"},{"Id":"6b7","Notes":"Testing 1","CreatedBy":"User2"}]
我使用上述方法填充模板:

<div class="create-note">
    <form>
        **<input type="hidden" id="Id" name="Id" value="{{this.Value.Id}}" />**       

        <textarea id="Notes" name="Notes"></textarea>       
        <button for="" type="submit" class="btn btn-primary ">Add Note</button>
        <button type="reset" class="btn">Cancel</button>
    </form>
            {{#unless this.Value.length}}
    <div class="alert alert-info">Notes do not exist.</div>
{{else}}
<table class="table">
    <thead>
        <tr>                        
            <th style="text-align:left">Note</th>
            <th style="text-align:left">Created By</th>
        </tr>
    </thead>
    <tbody>
        {{#each this.Value}}
            <tr>                
                <td>
                    {{this.Notes}}
                </td>
                <td>
                    {{this.CreatedBy}}
                </td>
            </tr>
        {{/each}}
    </tbody>
</table>
{{/unless}}
</div>

****       
添加注释
取消
{{{#除非这个.Value.length}
注释不存在。
{{else}
注
创建人
{{{#每个this.Value}
{{this.Notes}}
{{this.CreatedBy}}
{{/每个}}
{{/除非}

如何填充隐藏表单字段(Id)。value={{this.value.Id}}不起作用,因为我们有一个数组。

在JavaScript中拉出
Id
并将其放在需要的地方:

var data = {
    "Value": [
        {"Id": "6b7", "Notes": "testing", "CreatedBy": "User1"},
        {"Id": "6b7", "Notes": "Testing 1", "CreatedBy": "User2"}
    ]
};
data.Id = data.Value[0].Id;

var tmpl = Handlebars.compile($('#template').html());
var html = tmpl(data);
然后在模板中引用
{{this.Id}
(或者只引用
{{Id}}

演示:

或者,添加一个可以挖掘价值的助手,但这似乎是不必要的复杂性


如果你打算使用车把,你必须习惯于将你的数据按摩成一种车把友好的形式。车把的简单既有优点也有缺点。

你能做一些类似于
这个.Value[index].id的事情吗?虽然我觉得数组中的每个对象都有点有趣,尽管看起来是不同的对象,但它们都有相同的Id。数据就是这样的。所有记录的Id都相同,其他字段也不同。this.Value[index].Id会引发一个错误它会引发什么类型的错误?错误:第3行的Parse error:…gnId“Value=“{this.Value[0].Id}--------------------------------------------------期待'Id'谢谢你的回答。我对车把很陌生-我如何才能实现上述目标?