jQuery的下拉框应用

时间:2024-01-05 09:22:26

jQuery的下拉框应用

jQuery的下拉框左右选择应用

jQuery的下拉框应用

直接上代码

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title></title>
  6. <style type="text/css">
  7. * { margin:0; padding:0; }
  8. div.centent {
  9. float:left;
  10. text-align: center;
  11. margin: 10px;
  12. }
  13. span {
  14. display:block;
  15. margin:2px 2px;
  16. padding:4px 10px;
  17. background:#898989;
  18. cursor:pointer;
  19. font-size:12px;
  20. color:white;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div class="centent">
  26. <select multiple="multiple" id="select1" style="width:100px;height:160px;">
  27. <option value="1">选项1</option>
  28. <option value="2">选项2</option>
  29. <option value="3">选项3</option>
  30. <option value="4">选项4</option>
  31. <option value="5">选项5</option>
  32. <option value="6">选项6</option>
  33. <option value="7">选项7</option>
  34. </select>
  35. <div>
  36. <span id="add" >选中添加到右边>></span>
  37. <span id="add_all" >全部添加到右边>></span>
  38. </div>
  39. </div>
  40. <div class="centent">
  41. <select multiple="multiple" id="select2" style="width: 100px;height:160px;">
  42. <option value="8">选项8</option>
  43. </select>
  44. <div>
  45. <span id="remove"><<选中删除到左边</span>
  46. <span id="remove_all"><<全部删除到左边</span>
  47. </div>
  48. </div>
  49. <!-- 引入jQuery -->
  50. <script src="jquery.js" type="text/javascript"></script>
  51. <script type="text/javascript">
  52. $(function(){
  53. //移到右边
  54. $('#add').click(function() {
  55. //获取选中的选项,删除并追加给对方
  56. $('#select1 option:selected').appendTo('#select2');
  57. });
  58. //移到左边
  59. $('#remove').click(function() {
  60. $('#select2 option:selected').appendTo('#select1');
  61. });
  62. //全部移到右边
  63. $('#add_all').click(function() {
  64. //获取全部的选项,删除并追加给对方
  65. $('#select1 option').appendTo('#select2');
  66. });
  67. //全部移到左边
  68. $('#remove_all').click(function() {
  69. $('#select2 option').appendTo('#select1');
  70. });
  71. //双击选项
  72. $(document).on("dblclick", "#select1 option", function(){
  73. $(this).removeAttr("selected").appendTo('#select2');
  74. });
  75. //双击选项
  76. $(document).on("dblclick", "#select2 option", function(){
  77. $(this).removeAttr("selected").appendTo('#select1');
  78. });
  79. });
  80. </script>
  81. </body>
  82. </html>