加载中...

:checked


概述    checked selector

返回值:Array<Element(s)>

描述:匹配所有勾选的元素。

  • V : 1.0jQuery( ":checked" )

这个 :checked 选择器适用于复选框 (checkbox) ,单选框(radio button),和select元素的option元素。

对于检索select元素选中的选择项(option), 请使用 :selected 选择器。

示例

实例

查找所有选中的复选框元素

HTML 代码:
  1. <form>
  2. <input type="checkbox" name="newsletter" checked="checked" value="Daily" />
  3. <input type="checkbox" name="newsletter" value="Weekly" />
  4. <input type="checkbox" name="newsletter" checked="checked" value="Monthly" />
  5. </form>
jQuery 代码:
  1. $("input:checked")
结果:
  1. [ <input type="checkbox" name="newsletter" checked="checked" value="Daily" />, <input type="checkbox" name="newsletter" checked="checked" value="Monthly" /> ]

实例

确定有多少input元素是选中的.

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>checked demo</title>
  6. <style>
  7. div {
  8. color: red;
  9. }
  10. </style>
  11. <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
  12. </head>
  13. <body>
  14. <form>
  15. <p>
  16. <input type="checkbox" name="newsletter" value="Hourly" checked="checked">
  17. <input type="checkbox" name="newsletter" value="Daily">
  18. <input type="checkbox" name="newsletter" value="Weekly">
  19. <input type="checkbox" name="newsletter" value="Monthly" checked>
  20. <input type="checkbox" name="newsletter" value="Yearly">
  21. </p>
  22. </form>
  23. <div></div>
  24. <script>
  25. var countChecked = function() {
  26. var n = $( "input:checked" ).length;
  27. $( "div" ).text( n + (n === 1 ? " is" : " are") + " checked!" );
  28. };
  29. countChecked();
  30. $( "input[type=checkbox]" ).on( "click", countChecked );
  31. </script>
  32. </body>
  33. </html>

运行一下

实例

Identify the checked radio input.

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>checked demo</title>
  6. <style>
  7. input, label {
  8. line-height: 1.5em;
  9. }
  10. </style>
  11. <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
  12. </head>
  13. <body>
  14. <form>
  15. <div>
  16. <input type="radio" name="fruit" value="orange" id="orange">
  17. <label for="orange">orange</label>
  18. </div>
  19. <div>
  20. <input type="radio" name="fruit" value="apple" id="apple">
  21. <label for="apple">apple</label>
  22. </div>
  23. <div>
  24. <input type="radio" name="fruit" value="banana" id="banana">
  25. <label for="banana">banana</label>
  26. </div>
  27. <div id="log"></div>
  28. </form>
  29. <script>
  30. $( "input" ).on( "click", function() {
  31. $( "#log" ).html( $( "input:checked" ).val() + " is checked!" );
  32. });
  33. </script>
  34. </body>
  35. </html>

运行一下


还没有评论.