0

ここにjsfiddleがあります.jquery関数を使用して、テキスト入力に入力された数値と、質問ごとの「合計マーク」数値を減算する数値との間の計算を行うことができます。

jsfiddle で遊んでみてください。質問 1 でテキスト入力の値を変更すると、同じ質問ブロックで合計点数が変化することがわかります。質問 2 は読み取り専用のボックスですが、読み取り専用の理由は、単一の回答であるためです。質問の回答が 1 つの場合、テキスト入力は読み取り専用にする必要があります。複数の回答がある場合は、読み取り専用にするべきではありません。

実際には、jquery 関数を以下のコードで動作させたいのですが、現時点では動作しません。これらは、修正する必要がある私が抱えている問題です。

  • 合計点は 5 ではなく、 の値に等しい必要があります。$searchMarks[$key]問題ごとに合計点が異なる可能性があるためです。
  • 現時点では、以下のコードは計算を実行しません
  • 最後に、質問に単一の回答しか含まれていない場合、テキスト入力を読み取り専用にすることはありません。

私の質問は、jsfiddle とまったく同じように動作するように、以下のコードをどのように設定する必要があるかということです。

以下はコードです:

<script type="text/javascript" src="jquery/jquery-1.7.min.js"></script>
<script type="text/javascript" src="jquery/jquery-ui-1.8.16.custom.min.js"></script>
<script type="text/javascript" src="jquery/basic.js"></script>

<script type="text/javascript">

        $(document).ready(function() {

    var $inputs = $('input.individualMarks');

$inputs.filter(function() {
    return $(this).prop('readonly') === true;
}).each(function() {
    var $input = $(this);

});

$inputs.filter('[data-q_group]').keyup(function() {
    var $group = $inputs.filter('[data-q_group="' + $(this).data('q_group') + '"]');
    var $marks = $group.eq(0).closest('tr').find('td.noofmarkstd');
    var markVal = <?php $searchMarks ?>;
    $group.each(function() {
        markVal -= ($(this).val() || 0)
    })
    $marks.text(markVal)

})

});

    </script>   

    </head>

    <body>

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

    <?php 

    echo "<table border='1' id='markstbl'>
          <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</th>
          </tr>\n";
    $previous_question_id = null;
    $rowspans = array_count_values($searchQuestionId);
    foreach ($searchQuestionContent as $key=>$question) {

        // removed logic, not necessary to set empty strings if you're skipping them

        echo '<tr class="questiontd">'.PHP_EOL;

        if ($previous_question_id != $searchQuestionId[$key]) {
            echo '<td class="questionnumtd" name="numQuestion" rowspan="'.$rowspans[$searchQuestionId[$key]].'">'.htmlspecialchars($searchQuestionId[$key]).'</td>' . PHP_EOL;
            echo '<td class="questioncontenttd" rowspan="'.$rowspans[$searchQuestionId[$key]].'">'.htmlspecialchars($question).'</td>' . PHP_EOL;
        }

        echo '<td class="answertd" name="answers[]">';
        echo $searchAnswer[$key];
        echo '</td>' ;
        echo '<td class="answermarkstd"><input class="individualMarks" name="answerMarks[]" id="individualtext" type="text" /></td>' . PHP_EOL;

        if ($previous_question_id != $searchQuestionId[$key]) {
            echo '<td class="noofmarkstd" rowspan="'.$rowspans[$searchQuestionId[$key]].'">'.htmlspecialchars($searchMarks[$key]).'</td>' . PHP_EOL;
        }

        // moved this to the end
        if ($previous_question_id != $searchQuestionId[$key]) {
            $previous_question_id = $searchQuestionId[$key];
        }
    }
            echo '</tr>';
            echo "</table>" . PHP_EOL;

            ?>


    </form>
4

1 に答える 1

0

あなたのコードは、 $(document).ready で囲んだ後、ローカルで jsFiddle と同じように動作しました (上記のコメントで述べたように)。

$(function() {<your code>});

jquery を src するのを忘れたのではないでしょうか?

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
于 2012-11-08T04:19:00.140 に答える