簡體   English   中英

當“數據表”具有選中的復選框時如何啟用/禁用按鈕

[英]How to enable/disable a button when Datatables has checkbox selected row

當數據表中有選中的復選框行時,如何啟用/禁用按鈕?

var pctoreceive = $('#pcToReceive').DataTable({
           'columnDefs': [{
                'targets': 0,
                'searchable': false,
                'orderable': false,
                'className': 'dt-body-center',
                'render': function (data, type, full, meta) {
                    return '<input type="checkbox" name="id[]" value="'
                       + $('<div/>').text(data).html() + '">';
                }
            }],

我縮短了代碼。 上面顯示了我為checkbox選擇添加了新列

在此處輸入圖片說明

當沒有選定的行時,必須禁用這兩個按鈕。 否則啟用

$('#rc-select-all').on('click', function() {
    // Check/uncheck all checkboxes in the table
    var rows = pctoreceive.rows({
        'search': 'applied'
    }).nodes();
    $('input[type="checkbox"]', rows).prop('checked', this.checked);
});

// Handle click on checkbox to set state of "Select all" control
$('#pcToReceive tbody').on('change', 'input[type="checkbox"]', function() {
    // If checkbox is not checked
    if (!this.checked) {
        var el = $('#rc-select-all').get(0);
        // If "Select all" control is checked and has 'indeterminate' property
        if (el && el.checked && ('indeterminate' in el)) {
            // Set visual state of "Select all" control 
            // as 'indeterminate'
            el.indeterminate = true;
        }
    }
});

您的問題與數據表無關。

只需設置一個計數器變量來存儲檢查了多少輸入檢查,然后如果> 0,則啟用您的按鈕。

看到這個小例子(和小提琴

var counterChecked = 0;

$('body').on('change', 'input[type="checkbox"]', function() {

    this.checked ? counterChecked++ : counterChecked--;
    counterChecked > 0 ? $('#submitButton').prop("disabled", false): $('#submitButton').prop("disabled", true);

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM