代码之家  ›  专栏  ›  技术社区  ›  Milan Bastola

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

  •  0
  • Milan Bastola  · 技术社区  · 6 年前

    我有一组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>
    

    这是J小提琴: http://jsfiddle.net/humanware/rjfjp2mL/

    2 回复  |  直到 6 年前
        1
  •  1
  •   L0uis    6 年前

    你快到了。您的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>
    
        2
  •  1
  •   Vasile Florin Vilsan    6 年前

    var selecteddays=document.queryselectorall('.sessiondays input[type=“checkbox”]:checked'); var totaldays=selecteddays.length;

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