javascript - HTML 复选框生成选中或单击事件绑定(bind)

标签 javascript events input checkbox onclick

我已经通过js生成了复选框:

 anwsersCount = question.ChoiceQuestionAnwsers().length;
 questionBodyContainer = document.getElementById('questionBody');
            //self.ChoosedQuestionAnwsers = question.ChoiceQuestionAnwsers;
            for (var i = 0; i < anwsersCount; i++) {
                var newOption = document.createElement("input");
                var newOptionLabel = document.createElement("label");
                newOption.type = "checkbox";
                newOption.id = i;
                newOption.value = i;
                newOptionLabel.for = i;
                newOptionLabel.setAttribute("style", "margin-left: 5px");
                newOption.onclick = function(event) {
                    alert('alert');
                };
                newOptionLabel.innerHTML = question.ChoiceQuestionAnwsers()[i].Text;
               // questionBodyContainer.innerHTML += question.ChoiceQuestionAnwsers()[i].Text + "<p>";
               // questionBodyContainer.appendChild(newOption); 
                questionBodyContainer.appendChild(newOption);
                questionBodyContainer.appendChild(newOptionLabel);
                questionBodyContainer.innerHTML += "<p>";

                //self.ChoosedQuestionAnwsers.push(question.ChoiceQuestionAnwsers()[i]);
            }

生成的复选框的 onclick 事件不起作用。您有什么想法让它发挥作用吗?

最佳答案

在创建完它们之后尝试绑定(bind)它们:

anwsersCount = 5;
questionBodyContainer = document.getElementById('questionBody');

for (var i = 0; i < anwsersCount; i++) {
    var newOption = document.createElement("input");
    var newOptionLabel = document.createElement("label");
    newOption.type = "checkbox";
    newOption.id = i;
    newOption.value = i;
    newOptionLabel.for = i;
    newOptionLabel.setAttribute("style", "margin-left: 5px");
    newOptionLabel.innerHTML = "Dummie Text";
    questionBodyContainer.appendChild(newOption);
    questionBodyContainer.appendChild(newOptionLabel);
    questionBodyContainer.innerHTML += "<p>";
}

checks = questionBodyContainer.getElementsByTagName("input");
for (var i = 0; i < checks.length; i++)
{
    checks[i].addEventListener('click', function() {
          alert(this.getAttribute("id"));
    });
}

http://jsfiddle.net/LGcVU/

编辑:它在 for 循环内部不起作用的原因是因为您在更新 DOM 后使用了 innerHTML 属性。如果必须添加新段落,请不要通过该属性添加它,而是以与添加其他元素相同的方式添加它:

anwsersCount = 5;
questionBodyContainer = document.getElementById('questionBody');
for (var i = 0; i < anwsersCount; i++) {
    var newOption = document.createElement("input");
    var newOptionLabel = document.createElement("label");
    newOption.type = "checkbox";
    newOption.id = i;
    newOption.value = i;
    newOptionLabel.for = i;
    newOptionLabel.setAttribute("style", "margin-left: 5px");
    newOptionLabel.innerHTML = "Dummie Text";
    questionBodyContainer.appendChild(newOption);
    questionBodyContainer.appendChild(newOptionLabel);
    questionBodyContainer.appendChild(document.createElement("p"));
    newOption.addEventListener('click', (function()
                                         {
                                             alert(this.getAttribute("id"));
                                         }), false);
}

http://jsfiddle.net/hescano/LGcVU/4/

关于javascript - HTML 复选框生成选中或单击事件绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18475624/

相关文章:

javascript - 如何使用 NGINX 和 Node.js 提供静态文件(css、js、img)

javascript - CSS/Javascript <ul> 菜单弹出

c# - 如何检测 MDIClient 窗口何时滚动

ios - 如何将事件标题(从 UI TextView )添加到我的应用程序创建的日历事件中?

JavaScript:尝试理解计算指数值的递归函数的 Else 语句

javascript - javascript 如何检测函数的定义位置,顶部/底部?

javascript - 使用多个选择选项过滤数组

python - 如何确定按钮何时在 Tkinter 中发布?

html - 表单元素输入值中的换行符

Java:如何在控制台的一行中有两个或多个输入