dimanche 24 mai 2015

jQuery: Looping thru checkbox and storing in array?

I have a page with checkboxes on it, and I'm trying to loop through them when a button is clicked, check if any of them are checked, and if none are checked, I want to display an alert. This is what I have, and it isn't working.

       $(document).ready(function() { 
            $('input#addnewwebsite').click(function() {
                $('input[type=checkbox]').each(function() {
                    var categories = [];
                    var $(this) = $(this);    
                    if ($(this).is(':checked') == true) {
                        categories.push($(this)); // checked, add to array
                        break;
                    }
                    if (categories != null) {
                        alert('You must select at least one category.');
                        return false;
                    }
                });
            });
        });




Aucun commentaire:

Enregistrer un commentaire