简体   繁体   English

使用DOM和appendChild的JavaScript按钮onclick函数

[英]JavaScript button onclick function using DOM and appendChild

I've been trying to make a simple script, but I got stuck. 我一直在尝试制作一个简单的脚本,但是我陷入了困境。 It should work like this: User click the ADD button to add field for creating question, after that, user can click ADD ONE OPTION button , in order to create additional blank textareas below the question. 它应该像这样工作:用户单击“ ADD button以添加用于创建问题的字段,然后,用户可以单击“ ADD ONE OPTION button ,以便在问题下方创建其他空白文本区域。

This is the FIDDLE 这是FIDDLE

It's not working. 没用 Even I make an ADD button work, somehow, the other button stops, I got smth like Cannot call method 'appendChild' of null. 即使我使ADD按钮起作用,以某种方式,另一个按钮也停止了,我有点像无法调用null的方法'appendChild'。

This is the code: 这是代码:

HTML: HTML:

<input type="submit" value="ADD" onclick="addquestion();" />
<br/><br/>
<div id="below"></div><br/>

JAVASCRIPT: 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);
}

Here is fiddle check out its working fine. 这里是提琴检查出它的工作正常。

Updated: Javascript 更新: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.id="id_"+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);

    var btn=document.getElementById("id_"+n);
    if(btn.addEventListener){
        btn.addEventListener('click', function() { insertbelow(this)});    
    } else if(btn.attachEvent){ // IE < 9 :(
        btn.attachEvent('onclick', function() { insertbelow(this)});
    }
    n++;
}

function insertbelow(index) {
    index=index.id.split("_")[1];
    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);
}

When trying to add the onclick event you don't really give the function as handler, but the result of the execution of the function (nothing in your case). 尝试添加onclick事件时,您实际上并没有将函数作为处理程序,而是函数执行的结果 (在您的情况下什么也不做)。 This can solve your issue: 这可以解决您的问题:

addone.onclick = function() { insertbelow(n) };

您似乎收到了错误消息,因为您尝试定位ID为“ radiodown”的div,但这似乎不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM