使用JavaScript来添加表单字段,但会出现什么新的名字是什么?(Using javascrip

2019-10-16 14:11发布

阅读这篇文章后: 使用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()">

因此,将新的字段名称是什么? 我不明白足够的编码要弄清楚什么我告诉它来。 好东西,还有其他的聪明人在那里给我依靠!

感谢您的任何答案。

Answer 1:

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


Answer 2:

你可以这样做:

        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;
        ...


Answer 3:

您必须添加“name”属性为这个元素。 你可以这样做

newText.name = “newInput-” +计数器; //这是对方的回答说什么。

这样,你会在提交后收到newInput-0,newInput-1等服务器上



文章来源: Using javascript to add form fields, but what will the new name be?