0

Say I have the following line in my JSP:

<form:checkboxes path="appliedPlayers" items="${suitablePlayers}" itemValue="id" itemLabel="displayName" />

I would like to disable the form-submit button when none of the checkboxes are checked. Something like:

$('#checkboxes').change(function() { 
    if (none_are_checked)
        disableBtn();
});
peech
  • 931
  • 3
  • 12
  • 23
  • Possible duplicate of [How do I check if a checkbox is checked in jQuery?](http://stackoverflow.com/questions/901712/how-do-i-check-if-a-checkbox-is-checked-in-jquery) – Chirag Parmar Nov 11 '16 at 11:45

1 Answers1

0

Spring form tags does not support this. You can check the following link for supported attributes.

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/spring-form-tld.html#spring-form.tld.checkboxes

What can be done is, you can handle this scenario at the client side using jQuery(like you mentioned).

<script>
    $(document).ready(function(){
      $('input[name=players]').change(function() { 
       //alert('hello');
       var checkedNum = $('input[name="players[]"]:checked').length;
       if (!checkedNum) {
        // User didn't check any checkboxes
        disableBtn();
       }  
      });
    });
</script>

Explanation: in the above code snippet, when checkbox element changes then the registered function gets called which counts the number of selected checkbox elements. If zero then it enters if condition which is the requirement.

Note: above example assumes html attribute name value for checkboxes are players. You can change your jquery selectors appropriately if needed.

Credit: https://stackoverflow.com/a/16161874/5039001

Community
  • 1
  • 1
prem kumar
  • 5,641
  • 3
  • 24
  • 36