如何动态创建

| 我想在我的Web表单中动态创建输入类型的文本。更具体地说,我有一个文本字段,用户可以在其中输入所需的文本字段的数量;我希望文本字段以相同的形式动态生成。 我怎么做?     
已邀请:
使用JavaScript:
var input = document.createElement(\"input\");
input.type = \"text\";
input.className = \"css-class-name\"; // set the CSS class
container.appendChild(input); // put it into the DOM
    
使用Javascript,您只需要
document.createElement
setAttribute
var input = document.createElement(\"input\");
input.setAttribute(\'type\', \'text\');
然后,您可以使用
appendChild
将创建的元素附加到所需的父元素。
var parent = document.getElementById(\"parentDiv\");
parent.appendChild(input);
    
您可能正在寻找方法
document.createElement();
。     
您可以根据输入的文本字段的数量在循环中执行类似的操作。
$(\'<input/>\').attr({type:\'text\',name:\'text\'+i}).appendTo(\'#myform\');
但是为了获得更好的性能,我会先创建所有html并将其仅注入DOM一次。
var count = 20;
var html = [];
while(count--) {
  html.push(\"<input type=\'text\' name=\'name\", count, \"\'>\");
}
$(\'#myform\').append(html.join(\'\'));
编辑此示例使用jQuery附加html,但是您也可以轻松修改它以使用innerHTML。     
您可以使用
createElement()
方法创建该文本框     
我认为以下链接将为您提供帮助。如果要动态生成字段,并且还希望同时删除它们,则可以从此处获取帮助。我有同样的问题,所以我得到了答案
$(function() {
        var scntDiv = $(\'#p_scents\');
        var i = $(\'#p_scents p\').size() + 1;

        $(\'#addScnt\').live(\'click\', function() {
                $(\'<p><label for=\"p_scnts\"><input type=\"text\" id=\"p_scnt\" size=\"20\" name=\"p_scnt_\' + i +\'\" value=\"\" placeholder=\"Input Value\" /></label> <a href=\"#\" id=\"remScnt\">Remove</a></p>\').appendTo(scntDiv);
                i++;
                return false;
        });

        $(\'#remScnt\').live(\'click\', function() { 
                if( i > 2  ) {
                        $(this).parents(\'p\').remove();
                        i--;
                }
                return false;
        });
});
 http://jsfiddle.net/jaredwilli/tZPg4/4/     
请参阅此答案以获取非JQuery解决方案。只是帮助了我! 动态添加输入元素以形成表单     
这是我的解决方案
function fun() {
/*getting the number of text field*/
var no = document.getElementById(\"idname\").value;
/*text fields to be generated dynamically in the same form*/
  for(var i=0;i<no;i++) 
   {
    var textfield = document.createElement(\"input\");
    textfield.type = \"text\";
    textfield.id = \"idname4textfeild\"; 
    textfield.setAttribute(\"value\",\"\");
    document.getElementById(\'form\').appendChild(textfield);
  }
}
<form id=\"form\">
    <input type=\"type\" id=\"idname\" oninput=\"fun()\" value=\"\">
</form>
您可以使用ES6退出
  var inputs = [
        `<input type=\'checkbox\' id=\'chbox0\' onclick=\'checkboxChecked(this);\'> <input  type=\'text\' class=\'noteinputs0\'id=\'note` + 0 + `\' placeholder=\'task0\'><button  id=\'notebtn0\'                     >creat</button>`, `<input type=\'text\' class=\'noteinputs\' id=\'note` + 1 + `\' placeholder=\'task1\'><button  class=\'notebuttons\' id=\'notebtn1\' >creat</button>`, `<input type=\'text\' class=\'noteinputs\' id=\'note` + 2 + `\' placeholder=\'task2\'><button  class=\'notebuttons\' id=\'notebtn2\' >creat</button>`, `<input type=\'text\' class=\'noteinputs\' id=\'note` + 3 + `\' placeholder=\'task3\'><button  class=\'notebuttons\' id=\'notebtn3\' >creat</button>`, `<input type=\'text\' class=\'noteinputs\' id=\'note` + 4 + `\' placeholder=\'task4\'><button  class=\'notebuttons\' id=\'notebtn4\' >creat</button>`, `<input type=\'text\' class=\'noteinputs\' id=\'note` + 5 + `\' placeholder=\'task5\'><button  class=\'notebuttons\' id=\'notebtn5\' >creat</button>`, `<input type=\'text\' class=\'noteinputs\' id=\'note` + 6 + `\' placeholder=\'task6\'><button  class=\'notebuttons\' id=\'notebtn6\' >creat</button>`, `<input type=\'text\' class=\'noteinputs\' id=\'note` + 7 + `\' placeholder=\'task7\'><button  class=\'notebuttons\' id=\'notebtn7\' >creat</button>`, `<input type=\'text\' class=\'noteinputs\' id=\'note` + 8 + `\' placeholder=\'task8\'><button  class=\'notebuttons\' id=\'notebtn8\' >creat</button>`, `<input type=\'text\' class=\'noteinputs\' id=\'note` + 9 + `\' placeholder=\'task9\'><button  class=\'notebuttons\' id=\'notebtn9\' >creat</button>`
    ].sort().join(\" \");
   document.querySelector(\'#hi\').innerHTML += `<br>` +inputs;
<div id=\"hi\"></div>

要回复问题请先登录注册