加载中...

:radio


概述    radio selector

返回值:Array<Element(s)>

描述:选择所有类型为单选框的元素。

  • V : 1.0jQuery( ":radio" )

$(':radio')等价于$('[type=radio]')。如同其他伪类选择器(那些以“:”开始),建议使用此类选择器时,跟在一个标签名或者其它选择器后面,默认使用了全局通配符选择器 "*"。换句话说$(':radio') 等同于 $('*:radio'),所以应该使用$('input:radio')

要选择一个单选按钮相关的设置,你可以使用:$('input[name=gender]:radio')

Additional Notes(其他注意事项):

  • 因为:radio 是一个 jQuery 延伸出来的选择器,并不是的CSS规范的一部分,使用:radio 查询不能充分利用原生DOM提供的querySelectorAll() 方法来提高性能。为了当使用:radio 的时候在现代浏览器上获得更佳的性能,首先使用纯CSS选择器选择元素,然后使用[type="radio"]代替.

示例

实例

查找所有单选按钮

HTML 代码:
  1. <form>
  2. <input type="text" />
  3. <input type="checkbox" />
  4. <input type="radio" />
  5. <input type="image" />
  6. <input type="file" />
  7. <input type="submit" />
  8. <input type="reset" />
  9. <input type="password" />
  10. <input type="button" />
  11. <select><option/></select>
  12. <textarea></textarea>
  13. <button></button>
  14. </form>
jQuery 代码:
  1. $(":radio")
结果:
  1. [ <input type="radio" /> ]

实例

查找所有单选按钮。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>radio demo</title>
  6. <style>
  7. textarea {
  8. height: 25px;
  9. }
  10. </style>
  11. <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
  12. </head>
  13. <body>
  14. <form>
  15. <input type="button" value="Input Button">
  16. <input type="checkbox">
  17. <input type="file">
  18. <input type="hidden">
  19. <input type="image">
  20. <input type="password">
  21. <input type="radio" name="asdf">
  22. <input type="radio" name="asdf">
  23. <input type="reset">
  24. <input type="submit">
  25. <input type="text">
  26. <select>
  27. <option>Option</option>
  28. </select>
  29. <textarea></textarea>
  30. <button>Button</button>
  31. </form>
  32. <div></div>
  33. <script>
  34. var input = $( "form input:radio" )
  35. .wrap( "<span></span>" )
  36. .parent()
  37. .css({
  38. background: "yellow",
  39. border: "3px red solid"
  40. });
  41. $( "div" )
  42. .text( "For this type jQuery found " + input.length + "." )
  43. .css( "color", "red" );
  44. // Prevent form submission
  45. $( "form" ).submit(function( event ) {
  46. event.preventDefault();
  47. });
  48. </script>
  49. </body>
  50. </html>
运行一下


还没有评论.