加载中...

:checkbox


概述    checkbox selector

返回值:Array<Element(s)>

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

  • V : 1.0jQuery( ":checkbox" )

$(':checkbox') 等同于 $('[type=checkbox]')。如同其他伪类选择器(那些以“:”开始)建议前面加上一个标记名称或其他选择器;否则,默认使用通用选择("*")。换句话说$(':checkbox') 等同于 $('*:checkbox'),所以应该使用$('input:checkbox')来提升效率。

Additional Notes:(其他注意事项:)

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

示例

实例

查找所有复选框

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. $(":checkbox")
结果:
  1. [ <input type="checkbox" /> ]

实例

查找所有复选框

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>checkbox 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="checkbox">
  18. <input type="file">
  19. <input type="hidden">
  20. <input type="image">
  21. <input type="password">
  22. <input type="radio">
  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:checkbox" )
  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 the form from submitting
  45. $( "form" ).submit(function( event ) {
  46. event.preventDefault();
  47. });
  48. </script>
  49. </body>
  50. </html>
运行一下


还没有评论.