在Jquery / Ajax中使用for循环动态创建行

时间:2018-09-26 05:52:18

标签: jquery ajax for-loop

下面是代码,但输出出错,请帮帮我并输出屏幕截图: enter image description here

$.ajax({
        url : '/getquestions_info',
        type : "POST",               
        data : JSON.stringify(temp),
        contentType : 'application/json',
        success : function successmsg(data) {
          if(data.message){
            // alert(data.message);
            $location.path('/setupPage');
            $(".quiz-container").hide();
          }else{
              myQuestions=data;
              totQuestions = myQuestions.length;
              myQuestions.forEach((currentQuestion, questionNumber) => {
                const answers = [];
                i = 0;
                var content="<div class='row'>";
                for (letter in currentQuestion.options) {
                  if(currentQuestion.options[letter] != null){

                    if((i%2 == 0)){
                      content+=`</div><div class="row">` + `<div class="col-sm-6"><label>
                      <input type="radio" class="question_number" id="question${questionNumber}" ng-model="chkselct" name="${currentQuestion.question_id}" value="${currentQuestion.options[letter]}"><span style="margin-left:5px;position: relative;
                      bottom: 2px;">${currentQuestion.options[letter]}</span>
                    </label></div>`;
                    }
                    else{
                      content+=`<div class="col-sm-6"><label>
                      <input type="radio" class="question_number" id="question${questionNumber}" ng-model="chkselct" name="${currentQuestion.question_id}" value="${currentQuestion.options[letter]}"><span style="margin-left:5px;position: relative;
                      bottom: 2px;">${currentQuestion.options[letter]}</span>
                    </label></div>`;
                    }
                    if((i%2) !=0){
                      `</div><div class="row">`
                    }

                    answers.push(content);

                  }

                }
                i++;
                output.push(
                  `<div class="slide">
                     <div class="question"> ${currentQuestion.question_name} </div>
                     <div class="answers"> ${answers.join("")} </div>
                   </div>`
                );
              });
              quizContainer.innerHTML = output.join(""); 
          }          
        }
  });

0 个答案:

没有答案