我使用了来自http://viralpatel.net/blogs/dynamic-add-textbox-input-button-radio-element-html-javascript/ html-javascript/ 的代码。我的代码是这样的:
<HTML>
<HEAD>
<TITLE>Dynamically add Textbox, Radio, Button in html Form using JavaScript</TITLE>
<SCRIPT language="javascript">
function add() {
for (i=1; i<=5; i++)
{
//Create an input type dynamically.
var element = document.createElement("input");
//Assign different attributes to the element.
element.setAttribute("type", i);
element.setAttribute("name", i);
element.setAttribute("value", i);
var foo = document.getElementById("fooBar");
//Append the element in page (in span).
foo.appendChild(element);
}
}
</SCRIPT>
</HEAD>
<BODY>
<FORM>
<H2>Dynamically add element in form.</H2>
Select the element and hit Add to add it in form.
<BR/>
<INPUT type="button" value="Add" onclick="add()"/>
<span id="fooBar"> </span>
</FORM>
</BODY>
</HTML>
该代码有效。但不是水平添加它,我希望每个新文本字段都添加到一个新行中。我尝试在循环函数中使用</script><br/><script>
、<html><br/></html>
、document.write("\n")
和document.write("\r\n")
,但它不像我想要的那样工作。我应该怎么办?