如何在 Javascript 中动态添加文本框?

2022-01-13 00:00:00 textbox dom javascript

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

By default I have 5 textboxes. When a user clicks on a button, one textbox should be added.

我怎么能这样做?

推荐答案

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

If you replace the innerHTML, the previously entered values will be cleared, to avoid that, you can append the input elements programmatically:

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

检查 this 示例.

相关文章