如何在jQuery中插入id从1开始自动递增?info.name我使用下面的代码来获取聚合数据,例如,每个数据quessionId都有许多相关的reasons. 下面是我.js在 jQuery 中的部分代码:function fmtQuestionsByID(id,callback){ if(!DATA.questions[id] || !$('#card_'+id) )return; var project = DATA.projects[DATA.questions[id].projectId]; if(!project)return; var issueQuestionLists = DATA.alltags.reduce(function(a,b){ if(a[b['quessionId']]) { a[b['quessionId']].push({name:b['name'],color:b['color'],description:b['description'],reason:b['reason'],question:b['question'],issueId:b['issueId'],department:b['department'],_id:b['quessionId']}) } else{ a[b['quessionId']] = [{name:b['name'],color:b['color'],description:b['description'],reason:b['reason'],question:b['question'],issueId:b['issueId'],department:b['department'],_id:b['quessionId']}] } return a; },{}); for(var i=0;i < DATA.questions[id].tags.length;i++){ var lid = DATA.questions[id].tags[i]; for(var l in issueQuestionLists){ var lb = issueQuestionLists[l] for(var c=0;c< lb.length;c++){ var lc = lb[c]; if(lc._id == lid){ var info = lc; console.log('info', info); $('.tags_question').append('['+info.name+']' + info.description + '。' + 'Reason: '+info.reason+ '。' ||'[no data]' ); } } } }}我使用下面的 html 来获取上面的数据<div id="questioninfo">
<span class="tags_question"></span></div>在控制台中,我确实有这些词典,但为什么页面只得到一个词典?非常感谢您的帮助。非常感谢 Swati 的帮助,.html改为.append.
1 回答
九州编程
TA贡献1785条经验 获得超4个赞
您可以定义一个variable存储 count 值的值,然后每当您需要先打印它时increment,然后将其添加到您已经附加的数据中。因此您的代码将如下所示:
var count = 0; //declare this
for (var i = 0; i < DATA.questions[id].tags.length; i++) {
//..other codes
var lc = lb[c];
if (lc._id == lid) {
count++; //increment
var info = lc;
console.log('info', info);
//add with other datas
$('.tags_question').append(count + '[' + info.name + ']' + info.description + '。' + 'Reason: ' + info.reason + '。' || '[no data]');
}
}
- 1 回答
- 0 关注
- 91 浏览
添加回答
举报
0/150
提交
取消