【问题标题】:jQuery sometimes loads all the values but sometimes misses some valuesjQuery 有时会加载所有值但有时会丢失一些值
【发布时间】:2013-08-20 09:23:38
【问题描述】:

大家好,我正在使用 django-tastypie 进行问答。

在我的模板中,需要打印与某个主题相关的所有问题以及与特定问题相关的所有答案,为此我将一个对象传递给 jQuery 文件并在那里迭代该对象。

这是代码

$(data.quiztopics).each(function(index,element){  
   $(element.questions).each(function(index,question){  
     $(".quiz").append("<table id='question_"+question.id+">"+
                       "<tr><p name='question' id=question_"+question.id+">"+
                          (question.question_text) +
                       "</p></tr><tr id=answer_"+question.id+"></tr>"); 

$(question.answers).each(function(index,answer){
     $("#answer_"+question.id).append("<td>"+
        "<input type='radio' name='answer'id=answer_"+answer.id+">"+ 
            answer.answer_text +
        "</input></td>"); 
});

在 console.log 中,它显示一切正常,即与特定问题相关的答案。 bt 当我将它附加到表格行时,它错过了一些问题的答案 即

10+10

120 20  30  1
10*10

100 00  20  1
10/10

10-10

请告诉我解决方案我怎样才能将所有答案放在相关问题下

【问题讨论】:

    标签: javascript jquery tastypie


    【解决方案1】:

    似乎您没有关闭 table 标记,每次都添加,但从未关闭,这可能会 #$!!!$ 出现问题。您应该在“每个”之外添加标签,或者每次都关闭它。您的代码的其余部分看起来没问题。

    作为另一个建议...您应该修改数据结构以将答案作为问题的一个元素。这将解决您当前的问题,优化您的代码(无需在 dom 中使用第二个选择器和修改),并使您对自己感觉良好:)。

    【讨论】:

    • 是的,我错过了结束表标签。 bt 现在也不行了。实际上我想做的是我想将这些问题和答案附加到一个模板上,比如一个测验。我在提交测验时制作了多个表格并在每个表格 bcz 中分别回答 tr,我可以轻松地遍历测验 id 中的表格并从 td 中获取值。请告诉我是否有任何其他方法可以实现此测验显示和提交任务
    【解决方案2】:

    我已经解决了这个问题。可能是我有一些语法错误或其他东西。我删除了代码并重新编写了它,现在它可以完美运行了。
    新代码在这里。
    $(data.quiztopics).each(function(index,element){

                    $(element.questions).each(function(index,question){
                        $(".quiz").append("<table class= question_"+question.id+"><tr><td>"+question.question_text+"</td></tr></table>");
                        $(".question_"+question.id).append("<tr class=answer_"+question.id+"></tr>")
                        $(question.answers).each(function(index,answer){
                            if(question.question_type=="NUM"){
                                $(".answer_"+question.id).append("<td><input type=radio name="+question.id+"/> "+answer.answer_text+"</td>");
                            }
    
                        })
    
                    })
    

    但现在我的下一个问题是这是一个测验,我需要提交它。请告诉我如何遍历给定的问题和答案并选择特定答案来提交测验。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多