0

Нужно написать функцию, которая подсчитает количество нажатых checkbox

<html>
<head>
    <meta charset="UTF-8">
    <title>Тест</title>
</head>
<body bgcolor=rgb(300,200,102)>
<form name=f1>
<center>
<h1><font face="Verdana" color = rgb(100,150,100)> Представление информации</h1>
</center>
</font>
<table border=5 bordercolor=rgb(100,150,100) width=85% align=center bgcolor=rgb(255,250,154)>
<tr>
 <td>
 <h3>
 <center><font face="Verdana" color=brown><i>
 1. Полный набор символов,используемых для кодирования, называют:<br></i></font>
 </center>
 </h3>
  <h4><font color = rgb(100,150,100)>
  <form name="f1">
  <input type="checkbox" value="1">шрифтом<br>
  <input type="checkbox" value="1">алфавитом<br>
  <input type="checkbox" value="1">синтаксисом<br>
  <input type="checkbox" value="1">сематикой<br>
  </form>
  </h4>
</td>
 </tr>
<tr>
 <td>
 <h3>
 <center><font face="Verdana" color=brown><i>
 2. Минимальным объектом, используемым для кодирования текста, является:<br></i></font>
 </center>
 </h3>
  <h4><font color = rgb(100,150,100)>
  <form name="f2">
  <input type="checkbox" value="1">бит<br>
  <input type="checkbox" value="1">пиксель<br>
  <input type="checkbox" value="1">символ<br>
  <input type="checkbox" value="1">растр<br>
  </form>
  </h4>
</td>
 </tr>
<tr>
 <td>
 <h3>
 <center><font face="Verdana" color=brown><i>
 3. Для кодирования одной точки монохромного изображения, имеющего 256 оттенков, требуется:<br></i></font>
 </center>
 </h3>
  <h4><font color = rgb(100,150,100)>
  <form name="f3">
  <input type="checkbox" value="1">1 бит<br>
  <input type="checkbox" value="1">3 бита<br>
  <input type="checkbox" value="1">1 байт<br>
  <input type="checkbox" value="1">3 байта<br>
  </form>
  </h4>
</td>
 </tr>
</table>
    <center>
        <button>Проверить</button>
        <input type="reset" value="Стереть"><br /><br />
        Результат<br />
        <input type="text" name="result" readonly>
    </center>
</body>
</html>

2 ответа 2

5
document.querySelectorAll("input[type='checkbox']:checked").length
2

    var checks = document.querySelectorAll('input[type=checkbox]');


    check.onclick = function(){
        var count = 0;

        checks.forEach(function (c) {
            if (c.checked == true){
                count += 1;
            }
        });
        document.getElementById('result').value =count;
    };
<html>
<head>
    <meta charset="UTF-8">
    <title>Тест</title>
</head>
<body bgcolor=rgb(300,200,102)>
    <center>
        <h1><font face="Verdana" color = rgb(100,150,100)> Представление информации</h1>
    </center>
    </font>
    <table border=5 bordercolor=rgb(100,150,100) width=85% align=center bgcolor=rgb(255,250,154)>
        <tr>
            <td>
                <h3>
                    <center><font face="Verdana" color=brown><i>
                                1. Полный набор символов,используемых для кодирования, называют:<br></i></font>
                    </center>
                </h3>
                <h4><font color = rgb(100,150,100)>
                        <form name="f1">
                            <input type="checkbox" value="1">шрифтом<br>
                            <input type="checkbox" value="1">алфавитом<br>
                            <input type="checkbox" value="1">синтаксисом<br>
                            <input type="checkbox" value="1">сематикой<br>
                        </form>
                </h4>
            </td>
        </tr>
        <tr>
            <td>
                <h3>
                    <center><font face="Verdana" color=brown><i>
                                2. Минимальным объектом, используемым для кодирования текста, является:<br></i></font>
                    </center>
                </h3>
                <h4><font color = rgb(100,150,100)>
                        <form name="f2">
                            <input type="checkbox" value="1">бит<br>
                            <input type="checkbox" value="1">пиксель<br>
                            <input type="checkbox" value="1">символ<br>
                            <input type="checkbox" value="1">растр<br>
                        </form>
                </h4>
            </td>
        </tr>
        <tr>
            <td>
                <h3>
                    <center><font face="Verdana" color=brown><i>
                                3. Для кодирования одной точки монохромного изображения, имеющего 256 оттенков, требуется:<br></i></font>
                    </center>
                </h3>
                <h4><font color = rgb(100,150,100)>
                        <form name="f3">
                            <input type="checkbox" value="1">1 бит<br>
                            <input type="checkbox" value="1">3 бита<br>
                            <input type="checkbox" value="1">1 байт<br>
                            <input type="checkbox" value="1">3 байта<br>
                        </form>
                </h4>
            </td>
        </tr>
    </table>
    <center>
        <button id="check">Проверить</button>
        <input type="reset" value="Стереть"><br /><br />
        Результат<br />
        <input type="text" name="result" id="result" readonly>
    </center>
</body>

1
  • @Sam Smith Да не за что. Вариант Igor чуть проще и быстрее.
    – Denis640Kb
    23 фев 2020 в 17:24

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.