I have been adding the ability to delete multiple items in a ListView for a while now. One thing that has been bugging me though was the user could click the button to delete the selected items even if no items were selected which would popup a delete confirmation. If the user clicked yes, then a post back would occur and be confusing.
Demo | Download
End Product:
jQuery:
$(document).ready(function() {
checkButtonStatus();
$(".selectAll >input:checkbox").click(function() {
checkButtonStatus();
});
});
function checkButtonStatus() {
var count = 0;
$(".selectAll >input:checkbox").each(function() {
if (this.checked == true) {
count++;
}
});
if (count != 0) {
$("#ButtonDeleteSelected").removeAttr("disabled"); }
else {
$("#ButtonDeleteSelected").attr("disabled", "disabled");
}
}
Whenever a checkbox is checked or un-checked the function checkButtonStatus() is run. The function is also run on document ready. The function takes care of checking whether the button should be enabled or not.
Be sure to check out the Demo and the Download