在我的网页中,我在左侧有一个列表组项,并且右侧有一些字段。有了这个JSON,我怎么才能浏览fields.field数组来打印右侧的输入字段?我正在使用Handlebars.JS
{
"refs": {
"ref": [{
"alias": "alias1",
"fields": {
"field": [{
"libelle": "Libelle",
"name": "ref_libelle",
"type": "text",
"value": "value"
}, {
"libelle": "Alias",
"name": "ref_alias",
"type": "text",
"value": "value2"
}]
}
}]
}
}
答案 0 :(得分:0)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="calc-body">
<input type="text" id="calc-screen" disabled="disabled">
<!-- end of #calc-screen -->
<row class="buttons-row" id="first-row">
<input type="button" class="btn-clear" value="C">
<input type="button" class="btn-plus-negative" value="+-">
<input type="button" class="btn-divide" value="%">
<input type="button" class="btn-multiply" value="x">
</row>
<!-- end of .buttons #second-row -->
<row class="buttons-row" id="second-row">
<input type="button" class="btn-number" value="7">
<input type="button" class="btn-number" value="8">
<input type="button" class="btn-number" value="9">
<input type="button" class="btn-minus" value="-">
</row>
<!-- end of .buttons #second-row -->
<row class="buttons-row" id="third-row">
<input type="button" class="btn-number" value="4">
<input type="button" class="btn-number" value="5">
<input type="button" class="btn-number" value="6">
<input type="button" class="btn-minus" value="+">
</row>
<!-- end of .buttons #third-row -->
<row class="buttons-row" id="fourth-row">
<input type="button" class="btn-number" value="1">
<input type="button" class="btn-number" value="2">
<input type="button" class="btn-number" value="3">
<input type="button" class="btn-decimal" value=".">
</row>
<!-- end of .buttons #fourth-row -->
<row class="buttons-row" id="fifth-row">
<input type="button" class="btn-number" id="zero" value="0">
<input type="button" class="btn-equal" value="=">
</row>
<!-- end of .buttons #fifth-row -->
</div>
<!-- end of calc-body -->
[A-Za-z0-9]*
var name1= refs.ref[0].fields.field[0].name;
var val1 = refs.ref[0].fields.field[0].value;
如果您不知道键值的意思,只需获取JSON的键值并使用键值进行搜索。
答案 1 :(得分:0)
我不知道我是否正确,但可能使用路径
var data = {
"refs": {
"ref": [
{
"alias": "alias1",
"fields": {
"field": [
{
"libelle": "Libelle",
"name": "ref_libelle",
"type": "text",
"value": "value"
}, {
"libelle": "Alias",
"name": "ref_alias",
"type": "text",
"value": "value2"
}
]
}
}
]
}
};
var source = $("#entry-template").html();
var template = Handlebars.compile(source);
var html = template(data);
$('#foo').html(html);
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.6/handlebars.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script id="entry-template" type="text/x-handlebars-template">
{{#each refs.ref}}
{{#each fields.field}}
<div>{{libelle}}</div>
{{/each}}
{{/each}}
</script>
<div id="foo"></div>