[英]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:
这些是我需要解决的问题:
$searchMarks[$key]
, as each question could have a different total marks $searchMarks[$key]
,因为每个问题的总分可能不同 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.