使用javascript添加表单字段,但新名称是什么

Using javascript to add form fields, but what will the new name be?

本文关键字:新名 是什么 字段 javascript 添加 表单 使用      更新时间:2023-09-26

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

那么,新的字段名称将是什么?我对编码的理解还不够,无法弄清楚我在告诉它什么。还好还有其他聪明人可以依靠!

感谢您的任何回答。

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
你可以

这样做:

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

您必须为此元素添加"name"属性。你可以做类似的事情

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

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