Required attribute on multiple checkboxes with the same name?

This question already has an answer here:

  • Using the HTML5 “required” attribute for a group of checkboxes? 9 answers

  • Sorry, now I've read what you expected better, so I'm updating the answer.

    Based on the HTML5 Specs from W3C, nothing is wrong. I created this JSFiddle test and it's behaving correctly based on the specs (for those browsers based on the specs, like Chrome 11 and Firefox 4):

    <form>
        <input type="checkbox" name="q" id="a-0" required autofocus>
        <label for="a-0">a-1</label>
        <br>
    
        <input type="checkbox" name="q" id="a-1" required>
        <label for="a-1">a-2</label>
        <br>
    
        <input type="checkbox" name="q" id="a-2" required>
        <label for="a-2">a-3</label>
        <br>
    
        <input type="submit">
    </form>

    You can make it with jQuery a less lines:

    $(function(){
    
        var requiredCheckboxes = $(':checkbox[required]');
    
        requiredCheckboxes.change(function(){
    
            if(requiredCheckboxes.is(':checked')) {
                requiredCheckboxes.removeAttr('required');
            }
    
            else {
                requiredCheckboxes.attr('required', 'required');
            }
        });
    
    });
    

    With $(':checkbox[required]') you select all checkboxes with the attribute required, then, with the .change method applied to this group of checkboxes, you can execute the function you want when any item of this group changes. In this case, if any of the checkboxes is checked, I remove the required attribute for all of the checkboxes that are part of the selected group.

    I hope this helps.

    Farewell.


    i had the same problem, my solution was apply the required attribute to all elements

    <input type="checkbox" name="checkin_days[]" required="required" value="0" /><span class="w">S</span>
    <input type="checkbox" name="checkin_days[]" required="required" value="1" /><span class="w">M</span>
    <input type="checkbox" name="checkin_days[]" required="required" value="2" /><span class="w">T</span>
    <input type="checkbox" name="checkin_days[]" required="required" value="3" /><span class="w">W</span>
    <input type="checkbox" name="checkin_days[]" required="required" value="4" /><span class="w">T</span>
    <input type="checkbox" name="checkin_days[]" required="required" value="5" /><span class="w">F</span>
    <input type="checkbox" name="checkin_days[]" required="required" value="6" /><span class="w">S</span>
    

    when the user check one of the elements i remove the required attribute from all elements:

    var $checkedCheckboxes = $('#recurrent_checkin :checkbox[name="checkin_days[]"]:checked'),
        $checkboxes = $('#recurrent_checkin :checkbox[name="checkin_days[]"]');
    
    $checkboxes.click(function() {
    
    if($checkedCheckboxes.length) {
            $checkboxes.removeAttr('required');
        } else {
            $checkboxes.attr('required', 'required');
        }
    
     });
    
    链接地址: http://www.djcxy.com/p/56092.html

    上一篇: 如何在Javascript中设置HTML5所需的属性?

    下一篇: 多个复选框具有相同名称的必需属性?