简体   繁体   English

jQuery计算在jsfiddle示例中有效,但在主代码中无效

[英]jquery calculation works in jsfiddle example, but not in main code

I have a jsfiddle here where a jquery function is used in able to make a calculation between the number entered in the text input and subtracting the number with the "Total Marks" number per question. 这里有一个jsfiddle 其中使用了一个jquery函数,以便能够在文本输入中输入的数字与每个问题减去带有“总分”数字的数字之间的计算。

Have a play around in the jsfiddle, in question 1 change the values in the text input and you will see the total marks change with in the same question block. 在jsfiddle中玩一玩,在问题1中更改文本输入中的值,您将在同一问题块中看到总分数发生变化。 For question 2 it is a readonly box but the reason it is readonly is because it is a single answer. 对于问题2,它是一个只读框,但之所以是只读框是因为它是一个答案。 If a question has a single answer then the text input should be readonly else if multiple answers then it should not be readonly. 如果一个问题有一个答案,则文本输入应为只读;否则,如果文本有多个答案,则该文本输入不应为只读。

The fact is though is that I want the jquery function to work with the code below but it doesn't at the moment. 事实是,虽然我希望jquery函数与下面的代码一起使用,但目前还不行。 These are the problems I have which needs to be fixed: 这些是我需要解决的问题:

  • The total marks should not equal 5, it should equal the value of $searchMarks[$key] , as each question could have a different total marks 总分不应等于5,而应等于$searchMarks[$key] ,因为每个问题的总分可能不同
  • At the moment for the code below it does not perform the calculation 目前,下面的代码无法执行计算
  • Finally it is not making the text input readonly if question only contains single answer. 最后,如果问题仅包含单个答案,则不会使文本输入变为只读。

My question is that how should the code below be setup so that it can work exactly the same as the jsfiddle? 我的问题是,应如何设置以下代码,使其与jsfiddle完全相同?

Below is the code: 下面是代码:

<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>

Your code behaved the same locally as it did in jsFiddle after enclosing it within $(document).ready (As mentioned in a comment above). 将您的代码包含在$(document).ready中后,其本地行为与jsFiddle中的行为相同(如上面的评论中所述)。

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

Maybe you forgot to src jquery? 也许您忘记了src jquery?

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jsFiddle问题和相同的代码不适用于jQuery - Issue with jsFiddle and identical code not working with jQuery 具有相同代码/数据集的Highcharts代码在PHP中有效,但在JSfiddle中无效 - Highcharts code with same set of code/data works in PHP but does not work in JSfiddle 代码在jsfiddle中有效,但是当我将所有代码放入网站时不起作用 - Code works in jsfiddle but doesn't work when I put all the code into my website “静态”关键字如何工作? (一个代码示例) - How do “static” keyword works? (a code example) 代码在jsfiddle中工作,但在localserver中不工作 - Code is working in jsfiddle ,but not in localserver Bootstrap Collapse无法正常工作,但可以在JSFiddle中使用 - Bootstrap Collapse not working, but works in JSFiddle 代码可以在 jsfiddle 上运行,但不能在我的网站上运行吗? - code work on jsfiddle but not on my website? PHP图像上传:作为一个单独的示例,但并非来自我的项目代码 - PHP image upload: works as an isolated example but not from my project code 过渡链接可在jsfiddle中使用,但不适用于我的浏览器 - Transition links works in jsfiddle but not on my browser 在PHP中生成的Jquery代码,同时循环返回NaN进行计算 - Jquery code generated inside a PHP while loop returning NaN for the calculation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM