【问题标题】:javascript iterating array of objects using checkboxesjavascript使用复选框迭代对象数组
【发布时间】:2015-12-02 13:38:40
【问题描述】:

我想在checkbox 为真时显示div,在checkbox 为假时隐藏div。出于这个原因,我创建了一个对象数组,它们具有属性(复选框和 div)和值(复选框和 div 的选择器)。但是这段代码不起作用。我在哪里做错了?

https://jsfiddle.net/9LzLm9hx/9/

【问题讨论】:

    标签: javascript jquery html arrays checkbox


    【解决方案1】:

    只需将 .attr("checked") 更改为 .is(":checked")

    【讨论】:

      【解决方案2】:

      我不明白你为什么要给出这段无用的代码:

      <label for="common"></label> <span>Common</span>
      

      可以通过这种方式发挥作用:

      <label for="common"> <span>Common</span></label>
      

      为了解决您的问题,重命名复选框的 id 以匹配 divs 的类:

      <input type="checkbox" id="post-id" />
      <input type="checkbox" id="post-activation" />
      

      还有&lt;div&gt;s:

      <div class='col-xs-3 post-id'>id</div>
      <div class='col-xs-3 post-activation'>active</div>
      

      然后使用jQuery:

      $(function () {
          $("input:checkbox").change(function () {
              if (this.checked)
                  $("." + this.id).show();
              else
                  $("." + this.id).hide();
          });
      });
      

      【讨论】:

        猜你喜欢
        • 2017-03-29
        • 2019-02-27
        • 1970-01-01
        • 2014-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-16
        相关资源
        最近更新 更多