[英]jquery calculation works in jsfiddle example, but not in main code
我這里有一個jsfiddle ,其中使用了一個jquery函數,以便能夠在文本輸入中輸入的數字與每個問題減去帶有“總分”數字的數字之間的計算。
在jsfiddle中玩一玩,在問題1中更改文本輸入中的值,您將在同一問題塊中看到總分數發生變化。 對於問題2,它是一個只讀框,但之所以是只讀框是因為它是一個答案。 如果一個問題有一個答案,則文本輸入應為只讀;否則,如果文本有多個答案,則該文本輸入不應為只讀。
事實是,雖然我希望jquery函數與下面的代碼一起使用,但目前還不行。 這些是我需要解決的問題:
$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>
將您的代碼包含在$(document).ready中后,其本地行為與jsFiddle中的行為相同(如上面的評論中所述)。
$(function() {<your code>});
也許您忘記了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.