下面的代码能实现点击一个按钮一次增加三个文本框。但我想要三个文本框之间不要换行,三个文本框之后必须换行。即点一下按钮换一行。请高人指点一下。
<html>
<head>
<title>js动态的在页面上增加或删除一个文本框</title>
<script type="text/javascript">
<!--
var textNumber = 1;
function addTextBox(form, afterElement) {
// Increment the textbox number
textNumber++;
// Create the label
var label = document.createElement("label");
// Create the textbox
var textField = document.createElement("input");
textField.setAttribute("type","text");
textField.setAttribute("name","biaozhundaan_"+textNumber);
textField.setAttribute("id","biaozhundaan"+textNumber);
// Add the label's text
label.appendChild(document.createTextNode("第"+textNumber+"空: "));
// Put the textbox inside(里面)的//把文本框放进去
label.appendChild(textField);
// Add it all to the form fengshu
form.insertBefore(label,afterElement);
var label = document.createElement("labe1");
// Create the textbox
var textField = document.createElement("input");
textField.setAttribute("type","text");
textField.setAttribute("name","pingfengA"+textNumber);
textField.setAttribute("id","pingfengA"+textNumber);
// Add the label's text
label.appendChild(document.createTextNode("评分词"+textNumber+": "));
// Put the textbox inside(里面)的//把文本框放进去
label.appendChild(textField);
// Add it all to the form
form.insertBefore(label,afterElement);
var label = document.createElement("labe3");
// Create the textbox
var textField = document.createElement("input");
textField.setAttribute("type","text");
textField.setAttribute("name","fengshu"+textNumber);
textField.setAttribute("id","fengshu"+textNumber);
// Add the label's text
label.appendChild(document.createTextNode("分数"+textNumber+": "));
// Put the textbox inside(里面)的//把文本框放进去
label.appendChild(textField);
// Add it all to the form
//
form.insertBefore(label,afterElement);
return false;
}
function removeTextBox(form) {
if (textNumber > 1) { // If there's more than one text box
// Remove the last one added
form.removeChild(document.getElementById("txt"+textNumber).parentNode);
textNumber--;
}
}
//-->
</script>
<style type="text/css">
<!--block 此元素将显示为块级元素,此元素前后会带有换行符
label {display:block;}
-->
</style>
</head>
<body>
<form id="myForm" method="get" action="./" />
<label>Text Box #1a: <input type="text" name="txt1" id="txt1" /></label>
<p>
<input type="button" value="Add Textbox" onClick="addTextBox(this.form,this.parentNode); " />
<input type="button" value="Remove Textbox" onClick="removeTextBox(this.form)" />
</p>
<p><input type="Submit" value="Submit" /></p>
</form>
</body>
</html>