【问题标题】:How do I add textboxes dynamically in Javascript?如何在 Javascript 中动态添加文本框?
【发布时间】:2019-05-29 20:55:46
【问题描述】:

默认情况下,我有 5 个文本框。当用户单击按钮时,应添加一个文本框。

我怎么能这样做?

【问题讨论】:

    标签: javascript dom textbox


    【解决方案1】:

    如果您替换了 innerHTML,之前输入的值将被清除,为避免这种情况,您可以通过编程方式附加输入元素:

    var input = document.createElement('input'); 
    input.type = "text"; 
    //...    
    container.appendChild(input); 
    

    查看this 示例。

    【讨论】:

    • 正确答案 - 因为 innerHTML 将用新定义的 innerHTML 值替换指定 HTML 元素的内容。
    【解决方案2】:

    Javascript 函数

    function add() {
    
    //Create an input type dynamically.
    var element = document.createElement("input");
    
    //Create Labels
    var label = document.createElement("Label");
    label.innerHTML = "New Label";     
    
    //Assign different attributes to the element.
    element.setAttribute("type", "text");
    element.setAttribute("value", "");
    element.setAttribute("name", "Test Name");
    element.setAttribute("style", "width:200px");
    
    label.setAttribute("style", "font-weight:normal");
    
    // 'foobar' is the div id, where new fields are to be added
    var foo = document.getElementById("fooBar");
    
    //Append the element in page (in span).
    foo.appendChild(label);
    foo.appendChild(element);
    }
    

    HTML部分,

    <button id="button" value="Add" onClick:"javascript:add();">
    

    而且,完成了!

    【讨论】:

    • 这里可以设置文本框的属性。比如css属性、值、id等
    【解决方案3】:
    <script>
    function add()
    {
        document.getElementById("place").innerHTML="<input type='text' value=''>"
    }
    </script>
    <input type="button" value="clickMe" onClick="add();">
    <div id="place"></div>
    

    【讨论】:

    • 投了这个票的人不知道使用 innerHTML 的影响。
    【解决方案4】:
    <script>
    function add()
    {
        var inpt = document.getElementById('input_template');
        inpt.parentNode.appendChild(inpt.cloneNode(false));
    }
    </script>
    
    <input type="button" onclick="add();">
    

    将 id=input_template 设置为预定义的文本框之一

    【讨论】:

    • 我会将它设为一个单独的函数而不是内联函数,并且我会使用 CSS 来保持样式相同,但这看起来确实是一个聪明的方法,而且看起来确实如此会工作的。
    【解决方案5】:

    最好在按钮的onclick 上附加一个事件,这会将div 的可见性设置为inline。考虑到灵活性和稳健性,这是我能看到的最佳方式。

    有一个look here 示例代码。

    【讨论】:

      【解决方案6】:

      试试这个

      <html>
      <head>
      <title>Dynamic Form</title>
      <script language="javascript" type="text/javascript" >
      function changeIt()
      {
      
      createTextbox.innerHTML = createTextbox.innerHTML +"<br><input type='text' name='mytext' >"
      
      }
      </script>
      </head>
      <body>
      
      <form name="form">
      <input type="button" value="clickHere" onClick="changeIt()">
      <div id="createTextbox"></div>
      </form> 
      </body>
      </html>
      

      【讨论】:

      • 投了这个票的人不知道使用 innerHTML 的影响。
      • -1。当一个简单的appendChild() 工作时,与innerHTML 混在一起会不必要地慢,你的i 变量发生了什么?它甚至没有被使用,因为变量没有在innerHTML 字符串中展开,所以你的 HTML 变成了name='mytext',后面跟着浏览器无法理解的垃圾。
      • 写一次innerHTML就足够公平了; += 在 innerHTML 上总是一个错误。
      【解决方案7】:

      通过改变循环值我们可以动态生成文本框

      function addFunction() {
        var table = document.getElementById("textbox");
        var rowlen = table.rows.length;
        var row = table.insertRow(rowlen);
        row.id = rowlen;
        var arr = ['textboxfiledname'];
        for (i = 0; i < 2; i++) {
          var x = row.insertCell(i)
          if (i == 1) {
            x.innerHTML = "<input type='button' onclick='removeCell(" + row.id + ")' value=Delete>"
          } else {
            x.innerHTML = "<label>" + arr[i] + ":</label><input type='textbox' name='" + arr[i] + "'>"
          }
        }
      }
      
      function removeCell(rowid) {
        var table = document.getElementById(rowid).remove();
      }
      input[type=text] {
        width: 100%;
        height: 50%;
        padding: 15px;
        border: 1px solid #ccc;
        border-radius: 4px;
        resize: vertical;
      }
      
      label {
        padding: 12px 12px 12px 0;
        display: inline-block;
        font-family: sans-serif;
      }
      
      input[type=button] {
        background-color: #4CAF50;
        border: none;
        color: white;
        padding: 6px 20px;
        text-decoration: none;
        margin: 4px 2px;
        cursor: pointer;
      }
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      
      <fieldset style="margin-left:20%;margin-right:20%;font-family:sans-serif;padding:15px;border-radius:5px;background:#f2f2f2;border:5px solid #1F497D">
        <legend style="background:#1F497D;color:#fff;padding:5px 10px;font-size:22px;border-radius:5px;margin-left:20px;box-shadow:0 0 0 5px #ddd">DynamicTextbox</legend>
        <table id="textbox">
          <tr>
            <td>
              <input type="button" onclick="addFunction()" value="AddTextbox" />
            </td>
          </tr>
        </table>
      </fieldset>

      【讨论】:

        【解决方案8】:
        <!DOCTYPE html>
        <html>
        <body>
         <h1>My First Web Page</h1>
         <p>My first paragraph.</p>
         <button type="button" onclick="myFunction()">Click me!</button>
         <script>
         var count =0;
         function myFunction() {
         count++
          document.write('Lable1');
          document.write('<input type="text" name="one+count">');
          document.write('Lable2');
          document.write('<input type="text" name="two+count">');
        
          document.write('Lable3'); 
          document.write('<input type="text" name="three+count">');
        
          document.write('Lable4');
          document.write('<input type="text" name="four+count">');
        
          document.write(count);
        
          }
          </script>
        
          </body>
          </html> 
        

        【讨论】:

          猜你喜欢
          • 2014-08-02
          • 1970-01-01
          • 2010-12-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多