加载中...

:selected


概述

返回值:Array<Element(s)>

匹配所有选中的option元素

示例

实例

查找所有选中的选项元素

HTML 代码:
  1. <select>
  2. <option value="1">Flowers</option>
  3. <option value="2" selected="selected">Gardens</option>
  4. <option value="3">Trees</option>
  5. </select>
jQuery 代码:
  1. $("select option:selected")
结果:
  1. [ <option value="2" selected="selected">Gardens</option> ]

实例1

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>selected 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. <select name="garden" multiple="multiple">
  15. <option>Flowers</option>
  16. <option selected="selected">Shrubs</option>
  17. <option>Trees</option>
  18. <option selected="selected">Bushes</option>
  19. <option>Grass</option>
  20. <option>Dirt</option>
  21. </select>
  22. <div></div>
  23. <script>
  24. $( "select" )
  25. .change(function() {
  26. var str = "";
  27. $( "select option:selected" ).each(function() {
  28. str += $( this ).text() + " ";
  29. });
  30. $( "div" ).text( str );
  31. })
  32. .trigger( "change" );
  33. </script>
  34. </body>
  35. </html>

运行一下


还没有评论.