值只显示在只读文本框中

时间:2012-12-01 00:03:05

标签: php jquery html

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

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”列的值(对于上例中的readonly文本输入,则为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>

2 个答案:

答案 0 :(得分: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);
    }

答案 1 :(得分: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

我不确定这是不是你的问题,但如果不对,我会继续寻找。