2016-09-18 88 views
2

我正在學習只使用jQuery和PHP編寫測驗應用程序,沒有數據庫。我不能使用jquery爲標籤標籤設置html嗎?

我使用PHP來準備HTML文件的結構。然後使用jquery填充數據。

問題顯示正常。但選項的標籤標籤保持不定。

腳本:

var questions = [ 
 
{ 
 
    \t 
 
    \t "statement": "The function <img src='images/1.png'>, where [x] denotes the greatest integer function, is defined for all x &straightepsilon;", 
 
    \t "ans1": "R", 
 
    \t "ans2": "R - {(-1, 1) U n: n &straightepsilon; I }", 
 
    \t "ans3": "R<sup>+</sup> - (0, 1)", 
 
    \t "ans4": "R - (n: n &straightepsilon; N)", 
 
} 
 
]; 
 

 
$(document).ready(function() { 
 
\t var i = 0; 
 
\t var j = 1; 
 
\t while (i < questions.length) { 
 
\t \t $("#question" + j).html('Q' + j + ". " + questions[i]["statement"]); 
 
\t \t // the labels // 
 
\t \t $("#question" + j + "-choice1").html(questions[i]["ans1"]); 
 
\t \t //console.log($("#question" + j + "-choice1").html()); 
 
\t \t console.log(questions[i]["ans1"]); 
 
\t \t $("#question" + j + "-choice2").html(questions[i]["ans2"]); 
 
\t \t $("#question" + j + "-choice3").html(questions[i]["ans3"]); 
 
\t \t $("#question" + j + "-choice4").html(questions[i]["ans4"]); 
 
\t \t 
 
\t \t i++; 
 
\t \t j++; 
 
\t } 
 
});

下面是我使用創建標記的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++; 

    } 

?> 
+0

您在'name'屬性末尾缺少雙引號字符。 – Barmar

回答

2

你錯過了很多雙引號字符。例如。

'<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++; 
    } 
+0

它的工作。你能提出一個更好的方法來形成這樣的字符串,或者我應該更加小心嗎?非常感謝。 – Br34th7aking

+0

我已經使用可變插值而不是連接添加了我的首選樣式。 – Barmar