我的计算答案有问题。
对于问题中的每个答案,在文本输入中输入的内容都会减去该问题“总分”列下的数字与在该问题的文本输入中输入的数字。
示例如下:
在文本输入中,我输入了数字 1、1 和 2。Total Marks Remaining 列下的 Total Narks 是 5,但现在是 1,因为 5 - 1 - 1 - 2 = 1 Total Mark Remaining
我遇到的问题是,如果我将顶部文本输入更改为 3,那么计算现在应该是 5 - 3 - 1 - 2 = -1 剩余总标记。但它并没有这样做,因为它在 jquery 函数中的某处声明如果 Total Marks Remaining 数小于 0,则显示原始 Total Marks Remaining 数为 5。这是不正确的,它应该只显示减号数字,以便用户知道他们需要减少 1 分。如果 Total Marks Remaining 数小于 0,如何获得要显示的负数?
下面是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<?php echo $questionId; ?>_ans_org" class=
"q<?php echo $questionId; ?>_ans_org" value=
"<?php echo $searchMarks[$key]; ?>" /><input type="hidden" name=
"q<?php echo $questionId; ?>_ans" class=
"q<?php echo $questionId; ?>_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<?php echo $questionId; ?>_mark_0" q_group="1" name=
"answerMarks[]" id="individualtext" type="text" /></td><?php
if ($questionId != $prev_ques) {
?>
<td class="noofmarkstd q<?php echo $questionId; ?>_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>