mardi 29 mai 2018

jquery check checkbox of a certain criteria is checked

I have a JQuery 1.12 script that displays a message if a set of checkbox inputs are not chosen.

The code from here works fine with:

var checked = $('input[type=checkbox]:checked').length;
        if(!checked) {
            $("#satNavCheckText").slideDown();
            $("#satNavCheckText").focus();
            return false;
        }
        else {
            $("#satNavCheckText").slideUp();
            return false;
        }

The above works fine

BUT I now have to add new checkboxes to the same form and so I want to clarify the above code to only check certain set of checkboxes; namely those with the name='MaddsatNavValid' reference.

I have tried:

var checked = $('input[name="MaddsatNavValid"]:checkbox:checked').length;

and also

var checked = $('input[name="MaddsatNavValid"][type="checkbox"]:checked').length;

But the result is that the page constantly runs the else{...} query when any checkbox is ticked; whereas the return value of var checked should always be non-positive unless a checkbox of that name has been ticked.

I have seen similar questions on counting ALL checkboxes on a page or on selecting checkboxes by class or by id, etc. but I've not seen anything about how to do this or more specifically if there's an issue in the code shown.

I'm sure my issue is relatively simple for someone who knows. I have javascript. Javascript hates me.




Aucun commentaire:

Enregistrer un commentaire