【问题标题】:Using javascript to add form fields, but what will the new name be?使用 javascript 添加表单字段,但新名称将是什么?
【发布时间】:2012-04-09 23:00:22
【问题描述】:

看完这篇文章后: using javascript to add form fields.. but below, not to the side? 我已经让按钮工作了!但我不知道如何接收输出。 这是我输入的。

        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()">

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

感谢您的任何回答。

【问题讨论】:

    标签: javascript forms field add


    【解决方案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
    

    【讨论】:

      【解决方案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;
              ...
      

      【讨论】:

        【解决方案3】:

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

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

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

        【讨论】:

          猜你喜欢
          • 2023-04-08
          • 1970-01-01
          • 1970-01-01
          • 2014-12-03
          • 1970-01-01
          • 2019-03-21
          • 2012-03-31
          • 2011-04-03
          • 1970-01-01
          相关资源
          最近更新 更多