Loop through checkboxes and count each one checked or unchecked

前端 未结 5 1326
野性不改
野性不改 2020-11-30 22:40

I\'ve run into a bit of an issue. Here\'s a brief explanation.

I have 12 check boxes on a standard form. What I need to do is loop through each of them and learn whi

5条回答
  •  再見小時候
    2020-11-30 23:08

    Using Selectors

    You can get all checked checkboxes like this:

    var boxes = $(":checkbox:checked");
    

    And all non-checked like this:

    var nboxes = $(":checkbox:not(:checked)");
    

    You could merely cycle through either one of these collections, and store those names. If anything is absent, you know it either was or wasn't checked. In PHP, if you had an array of names which were checked, you could simply do an in_array() request to know whether or not any particular box should be checked at a later date.

    Serialize

    jQuery also has a serialize method that will maintain the state of your form controls. For instance, the example provided on jQuery's website follows:

    single=Single2&multiple=Multiple&multiple=Multiple3&check=check2&radio=radio2
    

    This will enable you to keep the information for which elements were checked as well.

提交回复
热议问题