2015-01-21 4 views
-3

이 프로그램에서는 첫 번째 입력 (텍스트 필드)이 길이를 나타내며 입력을 넣은 후 버튼을 클릭 한 후에 텍스트 필드의 수와 동일한 수의 텍스트 필드를 creaed합니다. 입력 길이. 내 문제는 내가 캡처 한 첫 번째 입력이지만 어떻게 동적으로 텍스트 필드를 만들지 모르겠다. 여기 내 코드입니다 :Idynamic에서 JavaScript로 입력 태그를 만드는 방법은 무엇입니까?

<html> 
    <body> 
     <input name="mioTesto" id="mioTesto" type="text" value="" size="40" maxlength="200" /> 
     <button onclick="catturaValore()">Inserisci Valore</button> 
     <script> 
      function catturaValore() { 

       var valore = document.getElementById("mioTesto").value; 
       var maxIndice = parseInt(valore); 

       var container = document.getElementById("container"); 
       for (var i = 0; i < maxIndice; i++) { 
        var input = document.createElement("input" + i) 
        input.name = "mioTesto" 
        input.id = i; 
        input.type = "text"; 
        input.size = "40"; 
        input.malength = "200"; 
        input.className = "css-class-name"; // set 
        container.appendChild(input); 
       } 
      } 
     </script> 
     <button onclick="ordinaVettore()">Ordina</button> 
    </body> 
</html> 
+0

태그에 번호를 추가하면 '+ i'가 제거됩니다. 유효하지 ? – adeneo

+0

@adeneo 동일하게 작동하지 않습니다! – user4480370

답변

0

변경이 줄을

var input=document.createElement("input"+i) 

실제로 유효한 요소 그래서

var input=document.createElement("input") 

합니다. # input1, # input2 등과 같이 그 이름이나 ID를 부여하고 싶다고 가정합니다 :

var input = document.createElement("input") 
    input.setAttribute('id', 'input'+i) 
+0

귀하의 제안에 따라 코드를 수정하지만 코드는 깨지지 않습니다! – user4480370

관련 문제