2012-11-16 85 views
0

我的计算答案有问题。jquery计算答案不正确

不断输入在文本输入每个答案的 问题是什么,它将减去 “总计标志”列下的数值与文本输入中输入了 这个问题的人数这个问题。

例子如下:

Screenshot showing how calculation works

在我在数字1,1和2的总体Narks输入的总标记剩余列下的文本输入为5,但它现在是1 5 - 1 - 1 - 2 = 1总剩余分数

我遇到的问题是,如果我改变顶部文本输入让可以说3,那么计算现在应该是5 - 3 - 1 - 2 = -1总计马克剩下。但它并没有这样做,因为jquery函数中的某个地方表明,如果Total Marks Remaining数字小于0,则显示原始的Total Marks Remaining数字是5.这是不正确的,它应该只显示减号因此用户知道他们需要减少1分。如果Total Marks Remaining数字小于0,我怎样才能得到负号码?

下面是jquery的函数的代码:

Jquery的:

$(function() { 
     //alert("here");   
     var questions = $('#markstbl td[class*="_ans"]').length - 1; 

     //disable single entry 
     for (var i = 0; i <= questions; i++) { 
      if ($("[class*=q" + i + "_mark]").length == 1) { 
       var t_marks = $("[class*=q" + i + "_ans]").html(); 
       //alert(t_marks); 
       $("[class*=q" + i + "_mark]").val(t_marks) 
        .attr("disabled", "disabled"); 
       //$("[class*=q"+i+"_mark]").attr("disabled","disabled"); 
      } 
     } 

     //find each question set and add listeners 
     for (var i = 0; i <= questions; i++) { 
      $('input[class*="q' + i + '"]').keyup(function() { 
       var cl = $(this).attr('class').split(" ")[1] 
       var questionno = cl.substring(cl.indexOf('q') + 1, cl.indexOf('_')); 
       var tot_marks = $(".q" + questionno + "_ans_org").val(); 
       //alert(tot_marks); 
       var ans_t = 0; 
       $("[class*=q" + questionno + "_mark]").each(function() { 
        var num = (isNaN(parseInt($(this).val()))) ? 0 : parseInt($(this).val()); 
        ans_t += parseInt(num); 
       }); 
       ans_t = tot_marks - ans_t; 
       //alert(ans_t); 
       //var fixedno = tot_marks; 
       var ans = (parseInt(ans_t) < 0) ? tot_marks : ans_t; 
       $(".q" + questionno + "_ans").val(ans); 
       $(".q" + questionno + "_ans_text").html(ans); 
      }); 
     } 
    });​ 

下面是动态HTML表:

HTML:

<body> 
    <table border='1' id='markstbl'> 
    <thead> 
     <tr> 
     <th class='questionth'>Question No.</th> 

     <th class='questionth'>Question</th> 

     <th class='answerth'>Answer</th> 

     <th class='answermarksth'>Marks per Answer</th> 

     <th class='noofmarksth'>Total Marks Remaining</th> 
     </tr> 
    </thead> 

    <tbody> 
     <?php 
     $row_span = array_count_values($searchQuestionId); 
     $prev_ques = ''; 
     foreach ($searchQuestionId as $key => $questionId) { 
     ?> 

     <tr class="questiontd"> 
     <?php 
      if ($questionId != $prev_ques) { 
     ?> 

     <td class="questionnumtd" name="numQuestion" rowspan= 
     "<?php echo $row_span[$questionId]; ?>"><?php 
       echo $questionId; 
     ?><input type="hidden" name="q&lt;?php echo $questionId; ?&gt;_ans_org" class= 
     "q&lt;?php echo $questionId; ?&gt;_ans_org" value= 
     "<?php echo $searchMarks[$key]; ?>" /><input type="hidden" name= 
     "q&lt;?php echo $questionId; ?&gt;_ans" class= 
     "q&lt;?php echo $questionId; ?&gt;_ans" value= 
     "<?php echo $searchMarks[$key]; ?>" /></td> 

     <td class="questioncontenttd" rowspan="<?php echo $row_span[$questionId]; ?>"> 
     <?php 
       echo $searchQuestionContent[$key]; 
     ?></td><?php 
      } 
     ?> 

     <td class="answertd" name="answers[]"><?php 
      echo $searchAnswer[$key]; 
     ?></td> 

     <td class="answermarkstd"><input class= 
     "individualMarks q&lt;?php echo $questionId; ?&gt;_mark_0" q_group="1" name= 
     "answerMarks[]" id="individualtext" type="text" /></td><?php 
      if ($questionId != $prev_ques) { 
     ?> 

     <td class="noofmarkstd q&lt;?php echo $questionId; ?&gt;_ans_text" q_group="1" 
     rowspan="<?php echo $row_span[$questionId]; ?>"><?php 
       echo $searchMarks[$key]; 
     ?></td><?php 
      } 
     ?> 
     </tr><?php 
      $prev_ques = $questionId; 
     } 
     ?> 
    </tbody> 
    </table> 
</body> 
</html> 
+0

可能重复的[Readolny文本框没有出现(http://stackoverflow.com/questions/13419707/readolny-textbox-is-not-appearing) –

回答

1

这行是肇事者:

var ans = (parseInt(ans_t) < 0) ? tot_marks : ans_t; 

更改为:

var ans = ans_t; 
+0

即我错了,我试图删除刚刚导致计算停止的那一行。谢谢 :) – user1819709