0

编码:

var i=0,j=0;

var t1= new Array();
function createtext(){

    i++;

    t1[i]=document.createElement('input');

    t1[i].type='text';

    t1[i].name='text'+i;

    t1[i].value = "hello"+i;

    t1[i].size = 10;

    document.forms[0].appendChild(t1[i]);

    var mybr=document.createElement("<br>");

    document.appendChild(mybr);

}

表格:

<form action="" method="get" name="f1">

    <input name="b1" type="button" onClick="createtext()" value="+">

</form>

单击“+”按钮时,文本框将向下生成而不是每个文本框的侧面。然后每个文本框在其侧面都有一个“删除”链接。当单击“删除”时,文本框将删除。请帮助我。

------------------------------------------------
(+) <-imagine this is the button

[SAMPLETEXTBOX1] REMOVE <-imagine a textbox1 with remove link beside it

[SAMPLETEXTBOX2] REMOVE <-imagine a textbox2 with remove link beside it

[SAMPLETEXTBOX1] REMOVE <-imagine a textbox3 with remove link beside it
4

3 回答 3

1

您应该附加到form,而不是document,也使用document.createElement("br")而不是document.createElement("<br>")

var mybr=document.createElement("br");
document.forms[0].appendChild(mybr);
于 2012-08-28T04:07:54.790 回答
0

此外,您可以将输入框和文本包装在 DIV 中,这将为您提供下面显示的控件所需的结果。

然后,当您单击删除时,您可以只删除 DIV 元素,它可以带走它的所有子节点(输入和文本)。

于 2012-08-28T04:03:02.933 回答
0

http://jsfiddle.net/hfdAL/9/

从您的 document.createElement 中删除 <>

于 2012-08-28T04:12:11.463 回答