以下是JSON的一部分
savedQuestionOptionsList: [
{
id: 3,
oid: {
id: 1,
name: "Excellent",
deleted: false,
version: 0
},
cqid: {
id: 19,
pqid: {
id: 9,
qid: {
id: 2,
category: "default",
question: "Staff Response",
cid: null,
deleted: false,
version: 0
},
pid: {
id: 2,
category: "default",
name: "Food And Beverage",
cid: null,
deleted: false,
version: 0
},
deleted: false,
version: 0
},
qtid: {
id: 4,
name: "option",
deleted: false,
version: 0
},
cid: {
id: 1,
name: "Taj",
createdDate: {
minimalDaysInFirstWeek: 1,
time: {
time: 1402030754000,
minutes: 29,
seconds: 14,
hours: 10,
month: 5,
timezoneOffset: -330,
year: 114,
day: 5,
date: 6
},
weekYear: 2014,
weeksInWeekYear: 52,
gregorianChange: {
time: -12219292800000,
minutes: 30,
seconds: 0,
hours: 5,
month: 9,
timezoneOffset: -330,
year: -318,
day: 5,
date: 15
},
timeZone: {
lastRuleInstance: null,
DSTSavings: 0,
rawOffset: 19800000,
ID: "Asia/Calcutta",
dirty: false,
displayName: "India Standard Time"
},
lenient: true,
timeInMillis: 1402030754000,
firstDayOfWeek: 1,
weekDateSupported: true
},
deleted: false,
version: 0
},
deleted: false,
version: 0
},
deleted: false,
version: 0
},
{
id: 4,
oid: {
id: 4,
name: "Poor",
deleted: false,
version: 0
},
cqid: {
id: 20,
pqid: {
id: 9,
qid: {
id: 2,
category: "default",
question: "Staff Response",
cid: null,
deleted: false,
version: 0
},
pid: {
id: 2,
category: "default",
name: "Food And Beverage",
cid: null,
deleted: false,
version: 0
},
deleted: false,
version: 0
},
qtid: {
id: 4,
name: "option",
deleted: false,
version: 0
},
cid: {
id: 1,
name: "Taj",
createdDate: {
minimalDaysInFirstWeek: 1,
time: {
time: 1402030754000,
minutes: 29,
seconds: 14,
hours: 10,
month: 5,
timezoneOffset: -330,
year: 114,
day: 5,
date: 6
},
weekYear: 2014,
weeksInWeekYear: 52,
gregorianChange: {
time: -12219292800000,
minutes: 30,
seconds: 0,
hours: 5,
month: 9,
timezoneOffset: -330,
year: -318,
day: 5,
date: 15
},
timeZone: {
lastRuleInstance: null,
DSTSavings: 0,
rawOffset: 19800000,
ID: "Asia/Calcutta",
dirty: false,
displayName: "India Standard Time"
},
lenient: true,
timeInMillis: 1402030754000,
firstDayOfWeek: 1,
weekDateSupported: true
},
deleted: false,
version: 0
},
deleted: false,
version: 0
},
deleted: false,
version: 0
}
],
要检索它,我使用以下jquery方式
$.each(data1.savedQuestionOptionsList, function(index, currQueOpt) {
console.log(currQueOpt.oid.id +"cqid id "+currQueOpt.cqid.id);
我有一张桌子
<tr>
<td><input id="checked2" type="checkbox" class="cd" value="2" style="margin:6px 0 0 0;"></td>
<td style="padding:6px 0 0 0;"><b>Staff Response</b></td>
<td>
<select name="questionType" id="questionType" class="qType QSelect" style="padding:3px;">
<option value="">--Select--</option>
<option value="1">text</option>
<option value="2">rating</option>
<option value="3">boolean</option>
<option class="show-checkboxes" value="4">option</option>
</select>
</td>
<td width="35%" style="padding:10px 0 0 0;" class="Fsize12 out" id="a2"><a href="#" class="checkbox-inline" onclick="getModal(this)">Get Options</a></td>
<td><input type="hidden" id="optionInputa2"></td>
</tr>
现在我的要求是将oid.name的值(在给定的JSON oid.name中给出2个优秀和差的值,并且pqid.qid.id值为2)附加到td
以上的类{}出去了。
我可以这样做
$.each(data1.savedQuestionOptionsList, function(index, currQueOpt) {
console.log(currQueOpt.oid.id +"cqid id "+currQueOpt.cqid.id);
$('#checked'+currQueOpt.cqid.pqid.qid.id).closest('tr').find('.out').html (currQueOpt.oid.name);
但是我必须在追加之前做一些处理,所以我不能直接在每个函数中使用。有没有办法存储qid.id和oid.name然后我可以循环和追加
答案 0 :(得分:0)
style="display: none"
创建另一个HTML块,并使用ID标记所有内部元素,在其中附加已解析的JSON值,然后使用jQuery选择并处理它。
此外,在$.each
语句中,您可以调用一些回调函数,该函数将首先处理您的数据,然后将其附加到需要的位置。
答案 1 :(得分:0)
不确定我是否理解你的需要,但这是我的两分钱:我认为最简单的方法就是创建一个辅助变量并存储在你想要的地方:
var aux=[];
$.each(data1.savedQuestionOptionsList, function(index, currQueOpt) {
console.log(currQueOpt.oid.id +"cqid id "+currQueOpt.cqid.id);
aux.push({name: currQueOpt.oid.name, id:currQueOpt.cqid.pqid.qid.id});
}
);
然后你可以处理这些值:
for (var i=0;i< aux.length;i++) {
//process aux[i].id
//process aux[i].name
}