我想通过点击按钮添加新的输入表单。 我有的形式,我想补充:
<input type="number" id="portdiv" name="ports" min="0" max="48" size="1"/>添加新表单的按钮:
<input type="button" value="Add another" onClick="addInput('portdiv');"/>javascript函数:
var counter = 1; function addInput(divName){ var textbox = document.createElement('input'); textbox.type = 'number'; document.getElementById(divName).appendChild(textbox); counter++; }此代码不起作用,我在javascript控制台上没有任何错误。 我已经尝试了不同的javascript函数解决方案:
var textbox = document.createElement('input'); textbox.type = 'text'; document.getElementById(divName).appendChild(textbox); counter++;和
var newFields = document.getElementById(divName).cloneNode(true); document.getElementById(divName).appendChild(newFields); counter++;他们中没有人工作。 怎么解决?
I would like to add new input form by clicking on a button. The form that I have and I would like to add:
<input type="number" id="portdiv" name="ports" min="0" max="48" size="1"/>The button to add the new form:
<input type="button" value="Add another" onClick="addInput('portdiv');"/>The javascript function:
var counter = 1; function addInput(divName){ var textbox = document.createElement('input'); textbox.type = 'number'; document.getElementById(divName).appendChild(textbox); counter++; }This code do not works and I have not any errors on javascript console. I have have tried different solution for the javascript function:
var textbox = document.createElement('input'); textbox.type = 'text'; document.getElementById(divName).appendChild(textbox); counter++;AND
var newFields = document.getElementById(divName).cloneNode(true); document.getElementById(divName).appendChild(newFields); counter++;Nobody of them works. How can be solved?
最满意答案
您正尝试将输入附加到输入! 输入没有子元素。
您需要将它附加到包含表单元素的父元素。 通过变量的名称,它将是某种div元素。 您可以通过div的名称来执行,也可以使用parentNode 。
You are trying to append an input to a input! An input does not have child elements.
You need to append it to the parent element that holds the form elements. By the name of your variable it would be some sort of div element. You can either do it by the name of the div, or use parentNode.
更多推荐
发布评论