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

前端 未结 8 1103
一向
一向 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.)

    0 讨论(0)
  • 2021-02-07 03:11

    Looking for something along these lines? http://jsfiddle.net/gXsZp/3/

    <div id="div1">
       Q1
      <input type="radio" name="r1" value="v1" />
      <input type="radio" name="r1" value="v2" />
      <input type="radio" name="r1" value="v3" />
      <br/>Q2
    
      <input type="radio" name="r2" value="v1" />
      <input type="radio" name="r2" value="v2" />
      <input type="radio" name="r2" value="v3" />
        <br/>Q3  
    
      <input type="radio" name="r3" value="v1" />
      <input type="radio" name="r3" value="v2" />
      <input type="radio" name="r3" value="v3" />
    </div>
    <br/>
    <input id="btn" type="submit" text="submit"/>
    
    
    
    $('#btn').click(function(){
        if ( $('#div1 input:radio:checked').size() == 3 )    
            return true;
        return false;
    });
    
    0 讨论(0)
提交回复
热议问题