ドキュメント

Selectors/radio

From jQuery JavaScript Library

Jump to: navigation, search

« Back to Selectors

:radio

Matches all input elements of type radio.
Using this pseudo-selector like $(':radio') is equivalent to $('*:radio') which is a slow selector. It's recommended to do $('input:radio').


To select a set of associated radio buttons, you might use: $('input[name=gender]:radio')
Examples:

Finds all radio inputs.

    var input = $("form input:radio").wrap('<span></span>').parent().css({background:"yellow", border:"3px red solid"});
    $("div").text("For this type jQuery found " + input.length + ".")
            .css("color", "red");
    $("form").submit(function () { return false; }); // so it won't submit

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  
  <script>
  $(document).ready(function(){
    
    var input = $("form input:radio").wrap('<span></span>').parent().css({background:"yellow", border:"3px red solid"});
    $("div").text("For this type jQuery found " + input.length + ".")
            .css("color", "red");
    $("form").submit(function () { return false; }); // so it won't submit

  });
  </script>
  <style>
  textarea { height:25px; }
  </style>
</head>
<body>
  <form>
    <input type="button" value="Input Button"/>
    <input type="checkbox" />
    <input type="file" />
    <input type="hidden" />
    <input type="image" />
    <input type="password" />
    <input type="radio" name="asdf" />
    <input type="radio" name="asdf" />
    <input type="reset" />
    <input type="submit" />
    <input type="text" />
    <select><option>Option<option/></select>
    <textarea></textarea>
    <button>Button</button>
  </form>
  <div>
  </div>
</body>
</html>

NameType