I want to know how many of the...">

How many flags checked

I have control buttons in a document.

<input type="checkbox" id="CustmerRequested"/>

I want to know how many of the boxes in the document are checked.

I tried to do alert(document.all.CustmerRequested.checked.length);it but it says undefined.

How to find out how many boxes are checked?

+3
source share
3 answers

If you are starting to build a site that requires this kind of programming on the browser side, I would suggest looking at jQuery. See here for tutorials: http://docs.jquery.com/Tutorials

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
    $(function() {
        $("#someButton").click(function() {
            var checkedBoxes = $("#yourForm input:checked");
            alert(checkedBoxes.length + " checked.");
        });
    });
</script>
+5
source

Use a Javascript structure like Prototype or JQuery to find the elements you need to check, for example. In the prototype:

var inputs = $$('input');

, , , :

for (var i=0; i<inputs.length; i++){
    if (inputs[i].type == 'checkbox' && inputs[i].checked) {
        numChecked++;
    }
}
+3

Yes, you can do this using JavaScript. No, you do not need jQuery.

Here is one way:

function howManyAreChecked()
{
one = document.getElementById("one").checked;
two = document.getElementById("two").checked;
three= document.getElementById("three").checked;

var checkCount = 0;
if ( one == true )
  { checkCount = checkCount + 1 }
if ( two == true )
  { checkCount = checkCount + 1 }
if ( three == true )
  { checkCount = checkCount + 1 }

alert(checkCount);
}

The above example assumes that you have 3 checkboxes in HTML, with the identifiers one, two, and three. The script first saves the value of the "checked" property, which can be TRUE or FALSE. The script then looks at each variable, and if TRUE, increments the counter.

0
source

Source: https://habr.com/ru/post/1751656/


All Articles