如何将 shiftcheckbox 添加到动态生成的复选框?

how to add shiftcheckbox to dynamic generated checkbox?

使用this jQuery shiftbox plugin plugin or 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>

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

您需要在附加元素后附加事件,因为您不能委托大多数 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();
}