我正在学习仅使用 jquery 和 PHP(不使用数据库)编写测验应用程序。
我使用 PHP 来准备 HTML 文件的结构。然后使用jquery来填充数据。
问题显示得很好。但选项的标签标签仍未定义。
脚本:
var questions = [
{
"statement": "The function <img src='images/1.png'>, where [x] denotes the greatest integer function, is defined for all x ϵ",
"ans1": "R",
"ans2": "R - {(-1, 1) U n: n ϵ I }",
"ans3": "R<sup>+</sup> - (0, 1)",
"ans4": "R - (n: n ϵ N)",
}
];
$(document).ready(function() {
var i = 0;
var j = 1;
while (i < questions.length) {
$("#question" + j).html('Q' + j + ". " + questions[i]["statement"]);
// the labels //
$("#question" + j + "-choice1").html(questions[i]["ans1"]);
//console.log($("#question" + j + "-choice1").html());
console.log(questions[i]["ans1"]);
$("#question" + j + "-choice2").html(questions[i]["ans2"]);
$("#question" + j + "-choice3").html(questions[i]["ans3"]);
$("#question" + j + "-choice4").html(questions[i]["ans4"]);
i++;
j++;
}
});
这是我用来创建标记的 php 脚本。
<?php
$numQues = 30; // max number of questions.
$i = 1;
while ($i <= $numQues) {
// careful with this string generator :)
echo '<div class="questions">
<p id="question' . $i . '"'. '></p>'.
'<input type="radio" name="ans' . $i .''. '><label id="question' . $i . '-choice1' .'"></label><br>'.
'<input type="radio" name="ans'. $i . '><label id="question' . $i . '-choice2' . '"></label><br>'.
'<input type="radio" name="ans'. $i . '><label id="question'. $i .'-choice3'.'"'.'></label><br>'.
'<input type="radio" name="ans' . $i . '><label id="question'. $i .'-choice4'.'"'.'></label><br>'.
'<button class="btn btn-primary pull-right" id="question-review'. $i . '"'. '>Mark for Review</button>
</div>';
$i++;
}
?>
最佳答案
您缺少很多双引号字符。例如
'<input type="radio" name="ans'. $i . '><label id="question' . $i . '-choice2' . '"></label><br>'.
应该是
'<input type="radio" name="ans'. $i . '"><label id="question' . $i . '-choice2' . '"></label><br>'.
^
因此,它将名称设置为类似 "ans1><label id="
的名称。 ,并且不认识<label>
作为新标签。
您可以在双引号字符串内使用变量插值,而不是所有容易出现问题的串联:
echo "<div class='questions'>
<p id='question$i'></p>
<input type='radio' name='ans$i'><label id='question$i-choice1'></label><br>
<input type='radio' name='ans$i'><label id='question$i-choice2'></label><br>
<input type='radio' name='ans$i'><label id='question$i-choice3'></label><br>
<input type='radio' name='ans$i'><label id='question$i-choice4'></label><br>
<button class='btn btn-primary pull-right' id='question-review$i'>Mark for Review</button>
</div>";
我将 HTML 元素中的引号更改为单引号,因此不需要转义它们。
另一种流行的方法是输出正常的HTML,然后使用<?php echo $variable ?>
在需要替换变量的地方。
while ($i <= $numQues) {
?>
<div class="questions">
<p id="question<?php echo $i; ?>"></p>
<input type="radio" name="ans<?php echo $i; ?>"><label id="question<?php echo $i; ?>-choice1"></label><br>
<input type="radio" name="ans<?php echo $i; ?>"><label id="question<?php echo $i; ?>-choice2"></label><br>
<input type="radio" name="ans<?php echo $i; ?>"><label id="question<?php echo $i; ?>-choice3"></label><br>
<input type="radio" name="ans<?php echo $i; ?>"><label id="question<?php echo $i; ?>-choice4"></label><br>
<button class="btn btn-primary pull-right" id="question-review<?php echo $i; ?>">Mark for Review</button>
</div>
<?
$i++;
}
关于php - 不能用jquery设置label标签的html吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39562283/