2012-04-09 28 views
1

看完這篇文章後: using javascript to add form fields.. but below, not to the side? 我已經做好了按鈕的工作!但我不知道如何接收輸出。 這就是我輸入的內容。 使用javascript添加表單域,但新名稱是什麼?

 var counter = 0; 
     function addNew() { 
      // Get the main Div in which all the other divs will be added 
      var mainContainer = document.getElementById('mainContainer'); 
      // Create a new div for holding text and button input elements 
      var newDiv = document.createElement('div'); 
      // Create a new text input 
      var newText = document.createElement('input'); 
      newText.type = "input"; 
      newText.maxlength = "50"; 
      newText.maxlimit = "50"; 
      newText.size = "60"; 
      newText.value = counter; 
      // Create a new button input 
      var newDelButton = document.createElement('input'); 
      newDelButton.type = "button"; 
      newDelButton.value = "Delete"; 

      // Append new text input to the newDiv 
      newDiv.appendChild(newText); 
      // Append new button input to the newDiv 
      newDiv.appendChild(newDelButton); 
      // Append newDiv input to the mainContainer div 
      mainContainer.appendChild(newDiv); 
      counter++; 

      // Add a handler to button for deleting the newDiv from the mainContainer 
      newDelButton.onclick = function() { 
        mainContainer.removeChild(newDiv); 
      } 
     } 
    </script> 

採用這種形式:

<input type="button" size="3" cols="30" value="Add" onClick="addNew()"> 

因此,將新的字段名稱是什麼?我對編碼不夠了解,不知道我在告訴它什麼。好東西有其他聰明的人在那裏讓我依靠!

感謝您的任何答案。

回答

3
newText.name = "newInputName-" + counter; // for name 
newText.id = "newInputId-" + counter; // for id 

對於按鈕

newDelButton.name = "btnDeleteName"; // for name, or "btnDeleteName-"+counter for multiple 
newDelButton.id = "btnDeleteId";  // for id or "btnDeleteId-"+counter for multiple 

NameId可能是相同的任何項目,即

newText.name = "newInput-" + counter; // for name 
newText.id = "newInput-" + counter; // for id 
1

你可以這樣做:

 var counter = 0; 
     function addNew() { 
     // Get the main Div in which all the other divs will be added 
     var mainContainer = document.getElementById('mainContainer'); 
     // Create a new div for holding text and button input elements 
     var newDiv = document.createElement('div'); 
     // Create a new text input 
     var newText = document.createElement('input'); 
     newText.type = "input"; 
     newText.id = "AddedInput_" + counter; 
     ... 
1

您必須添加「吶我「這個元素的屬性。你可以做類似

newText.name =「newInput-」+ counter; //這是另一個答案所說的。

這樣,您將在提交後在服務器上收到newInput-0,newInput-1等

相關問題