1

After reading this post: using javascript to add form fields.. but below, not to the side? I've made the button work! But I don't know how to receive the output. This is what I entered.

        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>

With this in the form:

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

So, what will the new field names be? I don't understand enough of the coding to figure out what I'm telling it to. Good thing there are other smart folks out there for me to lean on!

Thanks for any answers.

4

3 回答 3

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

Name并且Id对于任何项目都可以相同,即

newText.name = "newInput-" + counter; // for name
newText.id = "newInput-" + counter;   // for id
于 2012-04-09T23:05:37.897 回答
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;
        ...
于 2012-04-09T23:04:46.043 回答
1

您必须为此元素添加“名称”属性。你可以做类似的事情

newText.name = "newInput-" + 计数器;//这是另一个答案所说的。

这样,您将在提交后在服务器上收到 newInput-0、newInput-1 等

于 2012-04-09T23:09:33.810 回答