我遇到警告中显示的问题编号问题。它不是显示问题编号,而是将问题ID显示为问题编号。因此,在警报验证中,它表示例如:
You have errors on question number: 115
例如,应该说You have errors on question number: 3
现在此代码显示问题ID:value="<?php echo$questionId?>"
此代码显示问题编号:echo$searchQuestionNo[array_search($questionId, $searchQuestionId)]
但是当我尝试这样做时:
<input type='hidden' id='num_groups' name='num_groups' value='<?php echo$searchQuestionNo[array_search($questionId, $searchQuestionId)]`?>'>
它不显示jquery警报的问题编号。说实话,发生的事情是验证不起作用,因为它没有出现。然而,如果我将上述代码保留为问题ID的值是什么:
<input type='hidden' id='num_groups' name='num_groups' value='<?php echo$questionId?>'>
然后它会在警报中显示问题ID,警报实际上有效。
所以我的问题是如何在警报中包含问题编号值,同时还能使验证工作,就像对问题一样?
下面是主要代码,以便您可以看到所有内容的来源:
PHP:
<?
// This will hold the search results
$searchQuestionId = array();
$searchQuestionNo = array();
// Fetch the results into an array
// get result and assign variables (prefix with db)
$stmt->bind_result($dbSessionId, $dbSessionName, $dbQuestionId, $dbQuestionNo, $dbQuestionContent, $dbAnswer, $dbAnswerId, $dbQuestionMarks, $dbOptionId, $dbOptionType);
$searchQuestionId[] = $dbQuestionId;
$searchQuestionNo[] = $dbQuestionNo;
}
?>
</head>
<body>
<form id="PenaltyMarks" action="<?php echo htmlentities($action); ?>" method="post">
<?php
$ques_ans = array(); //to store incorrect answers against ques no.
$q_occ_count = array_count_values($searchQuestionId);
foreach($searchQuestionId as $key => $questionId)
{
.....
}
?>
<table id='penaltytbl'>
<thead>
<tr>
<th class='questionth'>Question No.</th>
<th class='questionth'>Question</th>
</tr>
</thead>
<tbody>
<?php
foreach($ques_ans as $questionId => $inc_ans)
{
?>
<tr class="questiontd">
<td class="questionnumtd q<?php echo$searchQuestionNo[array_search($questionId, $searchQuestionId)]?>_qnum" rowspan="<?php echo$q_row_span?>"><?php echo$searchQuestionNo[array_search($questionId, $searchQuestionId)]?>
<input type="hidden" name="numQuestion" value="<?php echo$questionId?>" />
</td>
<td class="answermarkstd">
<input class="individualMarks q<?php echo$questionId?>_mark" q_group="1" name="answerMarks[]" type="text" data-type="qmark" data-qnum="<?php echo$questionId?>" onkeypress="return isNumberKey(event)" maxlength="3" value="0" />
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<p>
<input type='hidden' id='num_groups' name='num_groups' value='<?php echo$questionId?>'>
<input id="submitBtn" name="submitPenalty" type="submit" value="Submit Marks" />
</p>
</form>
Jquery的:
< script type = "text/javascript" >
myClickHandler = function (e) {
var ng = $('#num_groups').val();
for (var group = 1; group
<= ng; group++) {
if (!validation(group)) return false;
}
});
function validation(group) {
var msg = [];
var nb = 0; // Number of blank values
$("input[data-qnum='" + group + "']").each(function () {
if ($(this).val() == '') {
nb++;
return false;
}
});
if (nb != 0) {
msg.push("\u2022 You have not entered in a value in all the Penalty Marks textbox \n");
}
if (msg.length >
0) {
alert("You have errors on Question Number: " + group + "\n\n" + msg.join("\n"));
return false;
} else {
return true;
}
}
< /script>
答案 0 :(得分:0)
我认为,正如你所说,这段代码允许解答问题编号:
echo$searchQuestionNo[array_search($questionId, $searchQuestionId)]
在输入元素中添加您的问题编号作为属性q_number
:
<input class="individualMarks q<?php echo$questionId?>_mark" q_number="<?php echo $searchQuestionNo[array_search($questionId, $searchQuestionId)]; ?>" q_group="1" name="answerMarks[]" type="text" data-type="qmark" data-qnum="<?php echo$questionId?>" onkeypress="return isNumberKey(event)" maxlength="3" value="0" />
并在validation
函数
function validation(group) {
var msg = [];
var qNumber = null;
var nb = 0; // Number of blank values
$("input[data-qnum='" + group + "']").each(function () {
//Assign the question number
qNumber = $(this).attr('q_number');
if ($(this).val() == '') {
nb++;
return false;
}
});
if (nb != 0) {
msg.push("\u2022 You have not entered in a value in all the Penalty Marks textbox \n");
}
//Use qNumber instead of the group variable
if (msg.length > 0) {
alert("You have errors on Question Number: " + qNumber + "\n\n" + msg.join("\n"));
return false;
} else {
return true;
}
}