Heim  >  Artikel  >  Web-Frontend  >  多个jquery.datatable共存,checkbox全选异常的快速解决方法_javascript技巧

多个jquery.datatable共存,checkbox全选异常的快速解决方法_javascript技巧

WBOY
WBOYOriginal
2016-05-16 17:10:03879Durchsuche

【问题原因】

这个应该是 jquery.datatable 控件本身的一个缺陷。该控件中的checkbox小插件的 id是写死的,所以当 有多个datatable 引用到一个页面中的时候,全选事件会匹配全部的datatable ,所以造成全部多个表格的 checkbox被都被选中。

【解决方法】

所以最好是修改jquery.datatable控件,给生成的每个datatable下的checkbox赋 予不同的id,因为datatable的id是不一样的,所以可以把 datatable的id作为 checkbox的前缀组成一个唯一的id 。  具体这个checkbox的调用事件也需要同步 替换成这个新id,进行事件的调用。

[修改文件]

jqurey.datatable.ext.js (v0.0.1)

1. init方法修改:

复制代码 代码如下:

$("#"+options.select_table).find('thead tr th:first-child')
         .prepend('');

==>

$("#"+options.select_table).find('thead tr th:first-child')
         .prepend('');


2.subscribeAllChk方法修改:
复制代码 代码如下:

$("#chk_all").click(function(){

==>

$("#"+$.fn.datatable_ext.defaults.select_table+"_chk_all").click(function(){


3.subscribeChk方法修改:
复制代码 代码如下:

if(checked_chk_num == curr_page_chk_num){
      $("#chk_all").attr('checked', 'checked');
     }else{
      $("#chk_all").removeAttr('checked');
     }

 

==>

if(checked_chk_num == curr_page_chk_num){
      $("#"+$.fn.datatable_ext.defaults.select_table+"_chk_all").attr('checked', 'checked');
     }else{
      $("#"+$.fn.datatable_ext.defaults.select_table+"_chk_all").removeAttr('checked');
     }

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn