【发布时间】:2014-04-02 10:32:07
【问题描述】:
我一直在尝试制作一个简单的脚本,但我卡住了。它应该像这样工作:
用户点击ADD button添加创建问题的字段,之后,用户可以点击ADD ONE OPTION button,以便在问题下方创建额外的空白文本区域。
它不工作。即使我让 ADD 按钮工作,不知何故,另一个按钮停止了,我得到了类似 Cannot call method 'appendChild' of null.
这是代码:
HTML:
<input type="submit" value="ADD" onclick="addquestion();" />
<br/><br/>
<div id="below"></div><br/>
JAVASCRIPT:
n=1;
function addquestion() {
var textarea = document.createElement("textarea");
textarea.name = "question[" + n + "]";
var addone = document.createElement("input");
addone.type = "button";
addone.value = "Add one option";
addone.onclick = "insertbelow("+n+")";
var div = document.createElement("div");
div.innerHTML = n + ". Question: <br />" + textarea.outerHTML + "<br />" + addone.outerHTML + "<br /><div id=\"radiodown"+n+"\" ></div><hr/><br/>";
document.getElementById("below").appendChild(div);
n++;
}
function insertbelow(index) {
var option = document.createElement("textarea");
option.name = "rad["+index+"]";
var optiondiv = document.createElement("div");
optiondiv.innerHTML = option.outerHTML + "<br/>";
document.getElementById("radiodown"+index).appendChild(optiondiv);
}
【问题讨论】:
标签: javascript html dom button