Jquery计算答案不正确

时间:2012-11-16 20:14:38

标签: php jquery html

我的计算答案有问题。

在a的每个答案的文本输入中输入了什么     问题,它将减去“总标记”列下的数字     对于该问题,在文本输入中输入数字     那个问题。

示例如下:

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 number小于0,则显示原始的Total Marks Remaining数字为5.这是不正确的,它应该只显示减去数字,以便用户知道他们需要减少1分。如果剩余总标记数小于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>

1 个答案:

答案 0 :(得分:1)

这条线是罪魁祸首:

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

将其更改为:

var ans = ans_t;