我试图创建一个复选框,一旦点击就会创建一个新的文本框。再次点击,它会删除文本框。复选框,添加或删除基于状态的文本框
目前,它只是加载新的文本框,因为我不知道如何处理javascript内部的语句。请有人指点我正确的方向。
<input id="chk" type="checkbox" value="results" /> Results
<div id="formContainer">
</div>
和JavaScript
function CreateTextbox() {
var textBox = document.createElement("input");
textBox.setAttribute("type", "textbox");
textBox.setAttribute("id", textboxId);
textboxId++;
return textBox;
}
var textboxId = 0;
if(textboxId == 0)
{
document.getElementById("chk").onclick = function()
{
document.getElementById("formContainer").appendChild(CreateTextbox(textboxId));
var textboxId = 1;
}
}
else if (textboxId == 1)
{
//The code to remove the previosuly made textbox
}
感谢堆,为需要的工作的。学到了新的东西! –
@MichaelN欢迎:) – pktangyue