jquery increase by 1
jquery increase by 1
下午,
对于显示的每个问题,我想将此代码中的数字增加1。
new_question = "<div id='"" + res.qId + "'">" +
"<h1>Question 1</h1>" +
"<p><i>" + res.question + "</i></p>" +
"<div class='"answerswer-grid'">" +
"<div id='"a1'" class='"answerswer'">" +
"<p>" + res.answer1 + "</p>" +
"<p>" + res.answer2 + "</p>" +
"<p>" + res.answer3 + "</p>" +
"</div>" +
"</div>";
有了这个,它会显示,问题1,问题2,等等…
请问我该如何在jQuery中进行此操作?
更新维克多提供了我需要的东西,感谢大家的帮助。更新了以下代码。
var questionNumber = 1;
$.each(result, function (index, res) {
new_question = "<div id='"" + res.qId + "'">" +
"<h1>Question " + (questionNumber++) + "</h1>" +
"<p><i>" + res.question + "</i></p>" +
"<div class='"answerswer-grid'">" +
"<div id='"a1'" class='"answerswer'">" +
"<p>" + res.answer1 + "</p>" +
"<p>" + res.answer2 + "</p>" +
"<p>" + res.answer3 + "</p>" +
"</div>" +
"</div>";
$('#pinfos').append(new_question);
在循环外声明一个变量,并在循环内递增,然后在标记中使用它。
var questionNumber=0;
$.each(someJsonData, function (index, res) {
{
questionNumber++;
new_question = "<div id='"" + res.qId + "'">" +
"<h1>Question "+questionNumber+"</h1>" +
"<p><i>" + res.question + "</i></p>" +
"<div class='"answerswer-grid'">" +
"<div id='"a1'" class='"answerswer'">" +
"<p>" + res.answer1 + "</p>" +
"<p>" + res.answer2 + "</p>" +
"<p>" + res.answer3 + "</p>" +
"</div>" +
"</div>";
}
声明一个名为questionNumber的var,并在每次调用时递增
var questionNumber = 1;
$.each(result, function (index, res) {
new_question = "<div id='"" + res.qId + "'">" +
"<h1>Question " + (questionNumber++) + "</h1>" +
"<p><i>" + res.question + "</i></p>" +
"<div class='"answerswer-grid'">" +
"<div id='"a1'" class='"answerswer'">" +
"<p>" + res.answer1 + "</p>" +
"<p>" + res.answer2 + "</p>" +
"<p>" + res.answer3 + "</p>" +
"</div>" +
"</div>";
$('#pinfos').append(new_question);
相关文章:
- getDocument by id/get input text from button dons'不起作用
- Div and ul list side by side
- TinySort sort by IDs
- Two input forms side-by-side programatically via JavaScript
- ngRepeat filter by array-property
- SQL应该查询's ORDER BY列写入JS代码中
- jQuery如何显示/隐藏Select by Select
- angularjs:自定义直接中的绑定数据在使用“”后不再更新;track-by-;在ng重复中
- jquery increase by 1
- Angular edit by clicking on a tr
- Vue.js"track-by-$index”;,如何单独呈现列表项
- filtet data by checkbox using jquery ajax mysql and php
- xml.getelement by tag name无法正常工作
- How to center tempAlert by Travis J
- toEqual by binding不起作用
- Contentful getEntries by multiple content_types
- selenium js webdriver.By
- 如何在 Sails.js 应用程序中禁用或替换 X-Powered-By 标头
- jQuery li element select by ID
- xml getElementsByTagName by variable