1

我已经通过 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 事件不起作用。你有什么想法如何让它发挥作用吗?

4

2 回答 2

0

代替

newOption.onclick = function(event) {
                    alert('alert');
                };

和:

newOption.addEventListener('click', function() {
  alert('alert');
});
于 2013-08-27T21:20:34.267 回答
0

全部创建后尝试绑定它们:

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 循环内不起作用的原因是因为您innerHTML在更新 DOM 后使用了该属性。如果您必须添加一个新段落,请不要通过该属性添加它,以与添加其他元素相同的方式添加它:

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/

于 2013-08-27T21:31:23.237 回答