【问题标题】:HTML checkbox generated checked or click event bind生成的 HTML 复选框选中或单击事件绑定
【发布时间】:2013-08-30 19:14:50
【问题描述】:

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

【问题讨论】:

    标签: javascript events input checkbox onclick


    【解决方案1】:

    替换

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

    与:

    newOption.addEventListener('click', function() {
      alert('alert');
    });
    

    【讨论】:

      【解决方案2】:

      在你创建它们之后尝试绑定它们

      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/

      【讨论】:

      • 完美!谢谢 :) 你能解释一下为什么这在新循环中有效吗?我正在学习 JS :)
      • 这在循环内不起作用的原因是因为在 DOM 中添加元素后,您通过添加具有 innerHTML 属性的新段落来修改 DOM。删除它,您应该能够在 for 循环中添加事件侦听器。 jsfiddle.net/hescano/LGcVU/2
      • 好的。感谢您的解释:)
      猜你喜欢
      • 2020-10-16
      • 2021-02-21
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 2023-03-13
      • 1970-01-01
      • 2014-07-20
      • 2018-03-19
      相关资源
      最近更新 更多