使用javascript显示所选复选框的列表

时间:2018-05-22 20:50:53

标签: javascript twitter-bootstrap checkbox bootstrap-4

我有一组7天的复选框,其常用名称为sessionDays。我使用此代码计算了选中的复选框:

var totalDays = document.querySelectorAll('.sessionDays input[type="checkbox"]:checked').length;

现在,我需要使用Javascript在HTML中显示所选复选框的列表。不是jQuery。我已经研究了很多问题,但没有找到解决方案。

这是我的HTML:

<div class="days sessionDays">
                    <label>Select Session Days</label>
                    <p class="text-muted">Select the specific days you want to come for training.</p>
                    <div class="form-check">
                      <input class="form-check-input weekend" type="checkbox" value="1" id="sessionSunday" name="sessionDays" onFocus="startCalc();" onBlur="stopCalc();" onclick="resetMorning();">
                      <label class="form-check-label" for="sessionSunday">
                        Sunday
                      </label>
                    </div>
                    <div class="form-check">
                      <input class="form-check-input weekday" type="checkbox" value="1" id="sessionMonday" name="sessionDays" onFocus="startCalc();" onBlur="stopCalc();" onclick="resetEvening();">
                      <label class="form-check-label" for="sessionMonday">
                        Monday
                      </label>
                    </div>
                    <div class="form-check">
                      <input class="form-check-input weekday" type="checkbox" value="1" id="sessionTuesday" name="sessionDays" onFocus="startCalc();" onBlur="stopCalc();" onclick="resetEvening();">
                      <label class="form-check-label" for="sessionTuesday">
                        Tuesday
                      </label>
                    </div>
                    <div class="form-check">
                      <input class="form-check-input weekday" type="checkbox" value="1" id="sessionWednesday" name="sessionDays" onFocus="startCalc();" onBlur="stopCalc();" onclick="resetEvening();">
                      <label class="form-check-label" for="sessionWednesday">
                        Wednesday
                      </label>
                    </div>
                    <div class="form-check">
                      <input class="form-check-input weekday" type="checkbox" value="1" id="sessionThursday" name="sessionDays" onFocus="startCalc();" onBlur="stopCalc();" onclick="resetEvening();">
                      <label class="form-check-label" for="sessionThursday">
                        Thursday
                      </label>
                    </div>
                    <div class="form-check">
                      <input class="form-check-input weekday" type="checkbox" value="1" id="sessionFriday" name="sessionDays" onFocus="startCalc();" onBlur="stopCalc();" onclick="resetEvening();">
                      <label class="form-check-label" for="sessionFriday">
                        Friday
                      </label>
                    </div>
                    <div class="form-check">
                      <input class="form-check-input weekend" type="checkbox" value="1" id="sessionSaturday" name="sessionDays" onFocus="startCalc();" onBlur="stopCalc();" onclick="resetMorning();">
                      <label class="form-check-label" for="sessionSaturday">
                        Saturday
                      </label>
                    </div>
                  </div>

这是JSFiddle:http://jsfiddle.net/humanware/rjfjp2mL/

2 个答案:

答案 0 :(得分:1)

你快到了。您的querySelectorAll调用将返回已检查控件的数组。 试试这段代码:

<html>
<head>
    <title>Random</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>

    <div class="days sessionDays">
        <label>Select Session Days</label>
        <p class="text-muted">Select the specific days you want to come for training.</p>
        <div class="form-check">
            <input class="form-check-input weekend" type="checkbox" value="1" id="sessionSunday" name="sessionDays"   onclick="whatsChecked();">
            <label class="form-check-label" for="sessionSunday">
                Sunday
            </label>
        </div>
        <div class="form-check">
            <input class="form-check-input weekday" type="checkbox" value="1" id="sessionMonday" name="sessionDays"  onclick="whatsChecked();">
            <label class="form-check-label" for="sessionMonday">
                Monday
            </label>
        </div>
        <div class="form-check">
            <input class="form-check-input weekday" type="checkbox" value="1" id="sessionTuesday" name="sessionDays"  onclick="whatsChecked();">
            <label class="form-check-label" for="sessionTuesday">
                Tuesday
            </label>
        </div>
        <div class="form-check">
            <input class="form-check-input weekday" type="checkbox" value="1" id="sessionWednesday" name="sessionDays"  onclick="whatsChecked();">
            <label class="form-check-label" for="sessionWednesday">
                Wednesday
            </label>
        </div>
        <div class="form-check">
            <input class="form-check-input weekday" type="checkbox" value="1" id="sessionThursday" name="sessionDays"  onclick="whatsChecked();">
            <label class="form-check-label" for="sessionThursday">
                Thursday
            </label>
        </div>
        <div class="form-check">
            <input class="form-check-input weekday" type="checkbox" value="1" id="sessionFriday" name="sessionDays"  onclick="whatsChecked();">
            <label class="form-check-label" for="sessionFriday">
                Friday
            </label>
        </div>
        <div class="form-check">
            <input class="form-check-input weekend" type="checkbox" value="1" id="sessionSaturday" name="sessionDays"  onclick="whatsChecked();">
            <label class="form-check-label" for="sessionSaturday">
                Saturday
            </label>
        </div>
    </div>
    <script type="text/javascript">
        function whatsChecked() {
            var foo = [];
            var things = document.querySelectorAll('.sessionDays input[type="checkbox"]:checked');
            for (var i = 0; i < things.length; i++) {
                foo[i] = things[i].id;
            }
            for (var i = 0; i < things.length; i++) {
                console.log(things[i].id);
            }
        }

    </script>
</body>
</html>

答案 1 :(得分:1)

var selectedDays = document.querySelectorAll(&#39; .sessionDays input [type =&#34; checkbox&#34;]:checked&#39;); var totalDays = selectedDays.length;

Array.from(selectedDays).(function(item, index){
// code to display every checkbox (item)
});