jquery: how to check if all radio buttons in a div are selected

前端 未结 8 1099
一向
一向 2021-02-07 02:13

my html looks like this

8条回答
  •  抹茶落季
    2021-02-07 03:02

    Validate the form when the user submits it, using this validation code.

    var blank = false;
    $("input:radio").each(function() {
        var val = $('input:radio[name=' + this.name + ']:checked').val();
        if (val === undefined) {
            blank = true;
            return false;
        }
    });
    alert(blank ? "At least one group is blank" : "All groups are checked");
    

    First we get the names of all the radio button groups, then check that each one has a value. (Actually we're doing multiple checks, but that doesn't really matter.)

提交回复
热议问题