簡體   English   中英

從JSON數組填充jQuery List,而JSON數組的一個元素為false

[英]populating jQuery List from JSON array while one element of the JSON Array false

我從Ajax調用中獲得了這個JSON對象,它將比一個對象大,但現在僅從一條記錄開始。

[{
 "SCENARIOID":"b9711a68-fb67-4b5c-bf79-a8a7a9a27df3",
 "USERID":"35be8bbb-99dc-44bc-9d84-9bcc937d79d6",
 "DateConfirmed":"\/Date(1434603186197)\/",
 "IsCorrect":true
}]

我的代碼中有一個<div id=test> </div> ,我想用JSON對象中的USERID填充一個無序列表,但僅當IsCorrect == true

我目前的嘗試是

$('#test').append('<ul id="fullCorrectByList"><ul/>');
$.each(response, function (key, data) {
    console.log(key);
    console.log(data);
    $.each(data, function (index, data) {
        if (index == 'IsCorrect' && data == true) {
            console.log('IsCorrect and True');
            //how do I console.log USERID for this json record?
        }
        console.log(index + ' : ' + data)
    })
})
console.log(response);

如果要處理的屬性是恆定的,則不需要第二個循環。

$('#test').append('<ul id="fullCorrectByList"><ul/>');
$.each(response, function (index, record) {
    console.log(index);
    console.log(record);
    if (record.IsCorrect) {
        console.log('correct', record.USERID)
    }
})
console.log(response);

如果要使代碼正常工作,則只需對變量進行唯一重命名即可訪問閉包值

 var response = [{ "SCENARIOID": "b9711a68-fb67-4b5c-bf79-a8a7a9a27df3", "USERID": "35be8bbb-99dc-44bc-9d84-9bcc937d79d6", "DateConfirmed": "\\/Date(1434603186197)\\/", "IsCorrect": true }]; $('#test').append('<ul id="fullCorrectByList"><ul/>'); $.each(response, function(index, record) { console.log(index); console.log(record); $.each(record, function(key, value) { if (key == 'IsCorrect' && value == true) { console.log('IsCorrect and True'); //how do I console.log USERID for this json record? console.log('USERID: '+record.USERID) } console.log(key + ' : ' + value) }) }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM