php - 不能用jquery设置label标签的html吗?

标签 php jquery

我正在学习仅使用 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 &straightepsilon;",
 	"ans1": "R",
 	"ans2": "R - {(-1, 1) U n: n &straightepsilon; I }",
 	"ans3": "R<sup>+</sup> - (0, 1)",
 	"ans4": "R - (n: n &straightepsilon; 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/

相关文章:

jquery - 根据需要设置 reCaptcha 字段

javascript - 在 Internet Explorer 中重置 Jquery 文件上传

PHP 在 PDO 语句准备时崩溃

php - 通过将记录存储到数组中获取表中缺失的 ID,并使用 for 循环比较该组数字

php - 如何加入产品和产品变体以获得带有产品名称的每个变体

javascript - jQuery 文档处理程序窃取输入字段中的按键

javascript - 使用 jQuery 限制文本区域中的行数和显示行数

php - 仅在生产服务器上找不到类错误

java - 从 java 到 php 的正则表达式转换

php - 如何在 jQuery 中使用 header 位置?