How can I enable/disable my button when there is a selected checkbox row in my datatable?
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() + '">';
}
}],
I've shorten my code. Above shows that I added a new column for checkbox
select
Those two button must be disabled when there is no selected row. Otherwise enable
$('#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;
}
}
});
Your issue has nothing to do with datatable.
Just set a counter variable to store how much input check are checked, then if > 0 enable your button.
See this small example (and fiddle )
var counterChecked = 0;
$('body').on('change', 'input[type="checkbox"]', function() {
this.checked ? counterChecked++ : counterChecked--;
counterChecked > 0 ? $('#submitButton').prop("disabled", false): $('#submitButton').prop("disabled", true);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.