javascript:点击添加表单(javascript: adding form on click)

编程入门 行业动态 更新时间:2024-10-28 16:29:30
javascript:点击添加表单(javascript: adding form on click)

我想通过点击按钮添加新的输入表单。 我有的形式,我想补充:

<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.

更多推荐

本文发布于:2023-07-27 22:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1296660.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   javascript   adding   click   form

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!