2014-01-22 56 views
0

我正在爲我的應用程序中的jquery數據表寫一個selectall按鈕。但按鈕行爲不一致,雙擊按鈕實際上取消選擇數據表中的複選框。這裏是我點擊按鈕事件的代碼。 sampleTable是我的jQuery數據表。用於在數據表中選擇全部的Jquery按鈕行爲。

$("#button_select_all").click(function() { 
     $(allSamplesTable.fnGetFilteredNodes()).find(':checkbox').each(function() { 
     $this = $(this); 
     $this.attr('checked', 'checked'); 

        for (var i = 0; i < _sampleTableExports.length; i++) { 
         if (_sampleTableExports[i]["Id"] == $(this).val()) { 
          _sampleTableExports.splice(i, 1); 
          return true; 
         } 
        } 
        _sampleTableExports.push({ "Name": $(this).attr("name"), "Id": $(this).val() }); 
       }); 

      }); 

請注意,爲什麼按鈕在第二次點擊時未勾選複選框。

+0

是'button_select_all'元素的一個按鈕還是一個複選框? – dcodesmith

+0

我把它作爲一個外部按鈕。 – user2501873

回答

0

而不是

$this.attr('checked', 'checked'); 

你可以嘗試

$this.prop('checked', true); 

如果你只是想選擇所有火一次嘗試這樣的:

$("#button_select_all").one("click", function() { 
    // Place your click logic in here 
     // $(allSamplesTable.fnGetFilteredNodes()).find...... 
}); 

但除此之外,它看起來像你的支票所有按鈕的行爲都是應該的,點擊一次選擇所有內容,然後再次單擊取消選擇所有內容。

+0

@GBP謝謝。我嘗試過使用它。它仍然取消了第二次點擊chekcboxes。它的翻轉開關每次點擊。 – user2501873

相關問題