【问题标题】:how to add shiftcheckbox to dynamic generated checkbox?如何将 shiftcheckbox 添加到动态生成的复选框?
【发布时间】:2015-08-12 08:11:36
【问题描述】:

使用this jQuery shiftbox plugin插件或this one

在 JS 中

for (var hidx in hosts) {
        var h = hosts[hidx];
        var line_html = '<tr><td><input type="checkbox" class="input shiftCheckbox"  value = "'+ hidx +'" data-fullname="'+ h +'"></input></td><td>' + h + '</td> </tr>';
        tbody_hosts.append($(line_html));
}

这是动态生成的。

在 HTML 中

<script src="/static/js/jquery.shiftcheckbox.js" type= "text/javascript" ></script>
<script type= "text/javascript" >       
    $(document).ready (function() {
        $('.shiftCheckbox').shiftcheckbox();
    });
</script>

为什么它不起作用?因为它是动态的?

【问题讨论】:

  • 您在复选框存在之前初始化您的 shiftcheckbox 功能。与其在 document.ready 块中声明它,不如在将动态内容添加到 dom 后找到一种添加方法。还有其他选择 - stackoverflow.com/questions/8690739/…

标签: javascript jquery checkbox shift


【解决方案1】:

您需要在附加元素后附加事件,因为您不能委托大多数 jquery 插件:

for (var hidx in hosts) {
    var h = hosts[hidx];
    var line_html = '<tr><td><input type="checkbox" class="input shiftCheckbox"  value = "'+ hidx +'" data-fullname="'+ h +'"></input></td><td>' + h + '</td> </tr>';
    tbody_hosts.append($(line_html));

    tbody_hosts.find('.shiftCheckbox').shiftcheckbox();
}

【讨论】:

  • 这就是我想要的,Zan!
  • @w.jingwei:很高兴它有帮助。不要忘记接受答案:)
猜你喜欢
  • 1970-01-01
  • 2013-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-15
  • 2016-02-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多