JQuery: enable submit if min 1 is checked

How to remove the attribute "disabled='disabled' submit button using id='bla' if at least one checkbox with class='check' checked?

If the check box is not selected, the disabled attribute should return to the submit button.

+7
source share
3 answers

You just need to check the length property of the checked array

 $('.check').change(function() { if ($('.check:checked').length) { $('#sub').removeAttr('disabled'); } else { $('#sub').attr('disabled', 'disabled'); } }); 

Here's the demo: http://jsfiddle.net/LUnN5/

+15
source

Get a link to all the relevant check boxes, and then set the disabled property in the change() event based on whether any of these check boxes are selected.

 var checks = $(':checkbox.check'); checks.change(function() { $('#bla').attr('disabled', ! checks.filter(':checked').length); }); 

jsFiddle .

+1
source
 $(".check").change(function() { var btn = $("#bla"); if ($(".check").is(":checked")) { btn.removeAttr("disabled"); } else { btn.attr("disabled", "disabled"); } }); $(".check").triggerHandler("change"); 
+1
source

All Articles