0

我在下面有一个代码,它在一列中显示文本输入,在第二列中显示总分列,在最后一列中显示剩余标记。该表如下所示:

Marks Per Answer               Total Marks    Marks Remaining
(blank text input)               4              4
(blank text input)               6              6
(blank disabled text input)      4              4

但我遇到的问题是,如果 READONLY 文本输入为空,它应该显示“Total Marks”列的值(上面示例中的只读文本输入为 4)。

此外,只读文本输入包含总分数,在“剩余分数”列下,这是不正确的,对于这一行,它应该显示 0。(至于只读文本输入包含值 4,所以如果我们减去4 在总标记下,它应该为禁用的文本输入行的剩余标记设为 0。)

所以当用户访问页面时,表格应该如下所示:

Marks Per Answer                                             Total Marks    Marks Remaining
(blank text input)                                                 4              4
(blank text input)                                                 6              6
(readonly text input = 4 (same value as under Total Marks))        4              0

我的问题是,如何通过更改下面的 jquery 来解决上述两个步骤:

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

        //disable single entry

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

        //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 = ans_t;
                var answerText = '<strong>' + ans + '</strong>';
                $(".q" + questionno + "_ans").val(ans);
                $(".q" + questionno + "_ans_text").html(answerText);
            });
        }
    });

下面是动态 HTML 表格:

<form id="Marks" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">


<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='totalmarksth'>Total Marks</th>
<th class='noofmarksth'>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="totalmarkstd" rowspan="<?php echo$row_span[$questionId]?>"><?php echo$totalMarks[$key]?></td>
<td class="noofmarkstd q<?php echo$questionId?>_ans_text"  q_group="1" rowspan="<?php echo$row_span[$questionId]?>"><?php echo"<strong>".$searchMarks[$key]."</strong>"?></td>
<?php
    }
    ?>
</tr>
<?php
$prev_ques = $questionId;
}
?>
</tbody>
</table>
</form>

目前在 jquery 函数中,我正在尝试使用 t_marks 变量来显示文本输入中的值,但是当我警告 t_marks 时没有显示任何内容

更新:

完整的 HTML:

<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='totalmarksth'>Total Marks</th>
<th class='noofmarksth'>Marks Remaining</th>
</tr>
</thead>
<tbody>

<tr class="questiontd">
        <td class="questionnumtd" name="numQuestion" rowspan="2">1 <input type="hidden" name="q1_ans_org" class="q1_ans_org" value="5"><input type="hidden" name="q1_ans" class="q1_ans" value="5"></td>
    <td class="questioncontenttd" rowspan="2">Here are 2 answers </td>
    <td class="answertd" name="answers[]">B</td>
<td class="answermarkstd">
<input class="individualMarks q1_mark_0"  q_group="1" name="answerMarks[]" id="individualtext" type="text" />
</td>
<td class="totalmarkstd" rowspan="2">5</td>
<td class="noofmarkstd q1_ans_text"  q_group="1" rowspan="2"><strong>5</strong></td>
</tr>

<tr class="questiontd">
    <td class="answertd" name="answers[]">D</td>
<td class="answermarkstd">
<input class="individualMarks q1_mark_0"  q_group="1" name="answerMarks[]" id="individualtext" type="text" />
</td>
</tr>

<tr class="questiontd">
        <td class="questionnumtd" name="numQuestion" rowspan="1">2 <input type="hidden" name="q2_ans_org" class="q2_ans_org" value="5"><input type="hidden" name="q2_ans" class="q2_ans" value="5"></td>
    <td class="questioncontenttd" rowspan="1">Here is a single answer </td>
    <td class="answertd" name="answers[]">True</td>
<td class="answermarkstd">
<input class="individualMarks q2_mark_0"  q_group="1" name="answerMarks[]" id="individualtext" type="text" />
</td>
<td class="totalmarkstd" rowspan="1">5</td>
<td class="noofmarkstd q2_ans_text"  q_group="1" rowspan="1"><strong>5</strong></td>
</tr>
</tbody>
</table>
4

2 回答 2

1

所以我认为这应该是你所需要的:

    if ($("[class*=q" + i + "_mark]").length == 1) {
        //alert(t_marks);
        var t_marks = $("[class*=q" + i + "_ans]");
        var t_marksHtml = t_marks.html();
        t_marks.html("0");
        $("[class*=q" + i + "_mark]").val(t_marksHtml).attr('readonly', true);
        //$("[class*=q"+i+"_mark]").attr('readonly',true);
    }
于 2012-12-01T01:31:20.787 回答
0

这可能会有所帮助:

$("[class*=q"+questionno+"_mark]").each(function(){

搜索名为的类q[X]_mark

这条线

<input class="individualMarks q<?php echo$questionId?>_mark_0"  q_group="1" name="answerMarks[]" id="individualtext" type="text" />

以这种方式编写课程:

q[X]_mark_0

我不确定这是否是您的问题,但如果不正确,我会继续寻找。

于 2012-12-01T00:18:39.160 回答