2017-08-26 40 views
-3

我有一個表格有從我必須爲每一個輸入多個ID和姓名輸入和元素

我怎樣才能防止這種類型的ID和用戶名參加考試的數百個輸入和元素? 有沒有解決方案的JavaScript?

<ol> 
     <li> 
      <div class="qselections"> 
       <input type="radio" value="1" id="1" name="question1"><label for="1">1</label> 
       <input type="radio" value="2" id="2" name="question1"><label for="2">2</label> 
       <input type="radio" value="3" id="3" name="question1"><label for="3">3</label> 
       <input type="radio" value="4" id="4" name="question1"><label for="4">4</label> 
      </div> 
     </li> 
     <br> 


     <li> 
      <div class="qselections"> 
       <input type="radio" value="1" id="5" name="question2"><label for="5">1</label> 
       <input type="radio" value="2" id="6" name="question2"><label for="6">2</label> 
       <input type="radio" value="3" id="7" name="question2"><label for="7">3</label> 
       <input type="radio" value="4" id="8" name="question2"><label for="8">4</label> 
      </div> 
     </li> 
     <br> 


. 
. 
. 
. 
. 


     <li> 
      <div class="qselections"> 
       <input type="radio" value="1" id="1197" name="question300"><label for="1197">1</label> 
       <input type="radio" value="2" id="1198" name="question300"><label for="1198">2</label> 
       <input type="radio" value="3" id="1199" name="question300"><label for="1199">3</label> 
       <input type="radio" value="4" id="1200" name="question300"><label for="1200">4</label> 
      </div> 
     </li> 
     <br> 


</ol> 
+0

不,你必須手工鍵入的一切,一個ID不能只是一個數字,你的情況應該是至少50個字符長,併爲每個元素完全獨特.... .......或者,您可以使用一種可以在循環中生成元素的服務器端語言。 – adeneo

回答

0

您可以使用for循環來動態生成元素。在這種情況下,您不必擔心身份證和姓名。

複製此代碼並粘貼到index.html文件,然後在Chrome瀏覽器中打開該文件。

<!DOCTYPE html> 
<html> 
<head> 
    <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script> 
    <meta charset="utf-8"> 
    <title>Test</title> 
    <meta name="description" content="Fullscreen backgrounds with centered content"> 
</head> 

<body> 

<ol id="questionArea"></ol> 

</body> 
<script type="text/javascript"> 
    var settings={ 
     questionCount:5, 
     subQuestionCount:4 
    }; 

    var htmlString=""; 
    var uniqueId = 1; 
    for(var i=0;i<settings.questionCount;i++){ 
     htmlString += "<li><div class='qselections'>"; 
     for(var j=1;j<=settings.subQuestionCount;j++){ 
      htmlString +="<input type='radio' value='"+j+"' id='"+ uniqueId +"' name='question"+ i +"'><label for='"+ uniqueId +"'"+">"+j+"</label>"; 
      uniqueId++; 
     } 
     htmlString += "</div></li>" 
    } 

    $(questionArea).html(htmlString); 
</script> 
</html> 
+0

再次感謝您,它運作良好,但有一個問題,首選項顯示0(零),但我想從1(一)開始。 –

+0

換句話說,我們有4個選擇從1開始==>(1 - 2 - 3 - 4)不是(0 - 1 - 2 - 3) –

+0

嗨,我已經更新了代碼。現在它將從1開始。 – Rakib