gpt4 book ai didi

javascript - 仅使用 JavaScript 将值从动态创建的文本框传递到数组

转载 作者:行者123 更新时间:2023-11-29 17:15:44 26 4
gpt4 key购买 nike

我想动态添加文本框,然后将这些文本框中写入的值传递给
数组。这是我的代码。

    function add(type)
{
a=new Array();
for(c=0;c<3;c++)
{
var element = document.createElement("input");
element.setAttribute("type", type);
element.setAttribute("value", type);
element.setAttribute("name", type);
element.setAttribute("id", 'n'+c);
var foo = document.getElementById("fooBar");
a[i]=document.getElementById('n'+c);
foo.appendChild(element);

}
}
</SCRIPT>
</HEAD>
<BODY>
<FORM>

<SELECT name="element">
<OPTION value="button">Button</OPTION>
</SELECT>
<INPUT type="button" value="Add" onclick="add(document.forms[0].element.value)"/>
<span id="fooBar">&nbsp;</span>

谢谢....

最佳答案

来试试这个!!!

var a= [];
function add(type) {
for(c=0;c<3;c++){
var element = document.createElement("input");
element.setAttribute("type", type);
element.setAttribute("value", type);
element.setAttribute("name", type);
element.setAttribute("id", 'n'+c);
var foo = document.getElementById("fooBar");
foo.appendChild(element);
a[c]=document.getElementById('n'+c).value;
}
}
function show(){
for(c=0;c<3;c++){
alert(a[c]);
}
}
</SCRIPT>
</HEAD>
<BODY>
<FORM>
<SELECT name="element">
<OPTION value="text">Text</OPTION>
</SELECT>
<INPUT type="button" value="Add" onclick="add(document.forms[0].element.value)"/><br>
<INPUT type=button name="b1" value="Show" onclick="show()"/><br>
<span id="fooBar">&nbsp;</span>

关于javascript - 仅使用 JavaScript 将值从动态创建的文本框传递到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18286851/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com