我有一个jsfiddle here
现在看看jsfiddledemo时遇到的问题是,如果你输入一个文本输入中的数字(问题1),它会进行计算,但它会在列中以不同的方式显示答案
让我们在顶部文本输入中输入数字2,然后它应显示“Marks Remaining 3”,但它会删除“Marks Remaining”txt并只显示数字3(而不是粗体)
所以我的问题是,为什么在显示计算答案时会改变远端列的格式?
以下是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<?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>
更新:
下面是第一次打开时页面的样子:
当我在文本输入中输入一个数字时,您可以看到右侧结束列的更改,它会进行计算并在结束列上显示答案:
答案 0 :(得分:1)
执行以下操作时:
$(".q" + questionno + "_ans_text").html(ans);
您正在用一个数字替换<strong>Marks Remaining:<br/>5</strong>
。一个简单的解决方案是添加另一个标记数字的标记,只更改该标记的文本。删除strong
标记是格式不同的原因
<strong>Marks Remaining:<br/><span class="ans">5</span></strong>
$(".q" + questionno + "_ans_text span.ans").html(ans);
或者你可以解析新的html:
var answerText='<strong>Marks Remaining:<br/>'+ans+'</strong>';
$(".q" + questionno + "_ans_text").html(answerText);