Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery复选框插件未在数据表分页时初始化_Jquery_Checkbox_Pagination_Datatables - Fatal编程技术网

jquery复选框插件未在数据表分页时初始化

jquery复选框插件未在数据表分页时初始化,jquery,checkbox,pagination,datatables,Jquery,Checkbox,Pagination,Datatables,我使用Switchery iPhone switch插件为复选框设置了一个Datatables表,其中一列非常有用。但是,当使用分页时,在第二个(或后续)页面上插件没有初始化。以下是开关站的初始化代码: var elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch')); elems.forEach(function(html) { var switchery = new Switc

我使用Switchery iPhone switch插件为复选框设置了一个Datatables表,其中一列非常有用。但是,当使用分页时,在第二个(或后续)页面上插件没有初始化。以下是开关站的初始化代码:

var elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch'));

    elems.forEach(function(html) {
      var switchery = new Switchery(html, { color: '#00a45a', secondaryColor: '#8f0a05' });
    });
我试过这个:

$('.dataTables_paginate ul.pagination li a').click(function() {
        var elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch'));

        elems.forEach(function(html) {
          var switchery = new Switchery(html, { color: '#00a45a', secondaryColor: '#8f0a05' });
        });
    });
但很明显,当返回到第一页时,它会重新初始化复选框,因此每次都有2个开关和一个额外的开关等

所以我试着在init上添加一个“switched”类,如下所示:

var elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch'));

    elems.forEach(function(html) {
      var switchery = new Switchery(html, { color: '#00a45a', secondaryColor: '#8f0a05' });
      $('.js-switch').each(function() {
        $(this).addClass('switched');
      });
    });
然后在paginate上过滤,如下所示:

$('.dataTables_paginate ul.pagination li a').click(function() {
        var elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch:not([class="switched"])'));
        console.log(elems);

        elems.forEach(function(html) {
          var switchery = new Switchery(html, { color: '#00a45a', secondaryColor: '#8f0a05' });
        });
    });
但这是行不通的,我相信这是一个简单的:not()?在此方面的任何帮助都将不胜感激

问候,


Rob.

使用类选择器,而不是使用属性选择器:

'.js-switch:not(.switched)'

您拥有的选择器是不可能的。或者,更确切地说,
:not()
部分总是匹配的,即使元素有一个
切换的
类。你是说,“给我一些元素,它们有一个'js switch'类,其class属性正好是'switched'。如果class属性正好是'switched',它就不能有一个'js switch'类。

如果你:1)更好地使用jQuery,你的代码会更容易理解(不是调用
querySelectorAll
Array.prototype.slice
)。2)使用函数封装永不更改的代码。这样,当您比较两个代码段时,我们就不会被没有引起问题的代码分散注意力,我们可以只关注重要的差异。太好了!谢谢就在我在这里的时候,这是我想要做的最好的方法还是你认为有更有效的方法?@RobMcKinney-看看dataTables提供的回调。然后,您可以只对新创建的行执行操作,而不是搜索整个文档,并且您根本不需要
:not()
选择器。