我可以显示JSON对象的元素,但是如果它在数组中,则会卡住。
这是我的代码:
$.ajax({
method: "GET",
url: api_url,
// url: mock_url,
contentType: "application/json",
data: adminPrefData,
success: function (res) {
console.log(res);
// callback(res);
$('#module').text(res.module); // i get this
$('#networkId').text(res.networkId); // and this
$('#adminPrefInfo.type').text(res.adminPrefInfo.type); // nothing happens on page
},
error: function () {
$('#info').html('<p>An error has occured</p>');
},
});
这是我的HTML:
<div class="content">
<div class="nav-link-content home">
<h1 class="text-center" id="module"></h1>
<div class="text-center" id="networkId"></div>
<div class="text-center" id="adminPrefInfo.type"></div>
</div>
</div>
这是我得到的JSON:
{"module":"dnd","networkId":1,"adminPrefInfo":[{"id":1,"key":"teszt","value":"teszt","defValue":"teszt1","type":"checkbox","isActive":true}]}
第一个问题是因为id
HTML元素的属性中有句点。您需要使用来在jQuery选择器中对其进行转义\\
。第二个问题是adminPrefInfo
数组。因此,您需要按索引访问它,然后获取type
属性。尝试这个:
$('#adminInfoList\\.type').text(res.adminPrefInfo[0].type);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句