test5.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Aisky-coffee</title>
  6. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  7. <!-- 新 Bootstrap4 核心 CSS 文件 -->
  8. <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.1.0/css/bootstrap.min.css">
  9. <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
  10. <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
  11. <!-- popper.min.js 用于弹窗、提示、下拉菜单 -->
  12. <script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js"></script>
  13. <!-- 最新的 Bootstrap4 核心 JavaScript 文件 -->
  14. <script src="https://cdn.bootcss.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
  15. <style>
  16. /*多選下拉框樣式(根據自己的樣式調整)*/
  17. .dropdown_item{
  18. width: 100%;
  19. }
  20. .dropdown_item>li:HOVER{
  21. background-color: #eee;
  22. cursor: pointer;
  23. }
  24. .dropdown_item>li {
  25. display: block;
  26. padding: 3px 10px;
  27. clear: both;
  28. font-weight: normal;
  29. line-height: 1.428571429;
  30. color: #333;
  31. white-space: nowrap;
  32. }
  33. .dropdown_item>li>.check_box{
  34. width: 18px;
  35. height: 18px;
  36. vertical-align: middle;
  37. margin: 0px;
  38. }
  39. .dropdown_item>li>span{
  40. vertical-align: middle;
  41. }
  42. .select_multiple .caret{
  43. border-top: 4px solid!important;
  44. border-bottom: 0;
  45. }
  46. </style>
  47. <script>
  48. //多選下拉框實現
  49. $(document).on("click",".check_box",function(event){
  50. event.stopPropagation();//阻止事件冒泡,防止觸發li的點選事件
  51. //勾選的項
  52. var $selectTextDom=$(this).parent().parent("ul").siblings("button").children(".select_text");
  53. //勾選項的值
  54. var $selectValDom=$(this).parent().parent("ul").siblings(".select_val");
  55. //是否有選擇項了
  56. var isSelected=$selectTextDom[0].getAttribute("data-is-select");
  57. var selectText="";//文字值,用於顯示
  58. var selectVal=$selectValDom.val();//實際值,會提交到後臺的
  59. var selected_text=$(this).siblings("span").text();//當次勾選的文字值
  60. var selected_val=$(this).val();//當次勾選的實際值
  61. //判斷是否選擇過
  62. if(isSelected=="true"){
  63. selectText=$selectTextDom.text();
  64. }
  65. if(selectText!=""){
  66. if(selectText.indexOf(selected_text)>=0){//判斷是否已經勾選過
  67. selectText=selectText.replace(selected_text,"").replace(",,",",");//替換掉
  68. selectVal=selectVal.replace(selected_val,"").replace(",,",",");//替換掉
  69. //判斷最後一個字元是否是逗號
  70. if(selectText.charAt(selectText.length - 1)==","){
  71. //去除末尾逗號
  72. selectText=selectText.substring(0,selectText.length - 1);
  73. selectVal=selectVal.substring(0,selectVal.length - 1);
  74. }
  75. }else{
  76. selectText+=","+selected_text;
  77. selectVal+=","+selected_val;
  78. }
  79. }else{
  80. selectText=selected_text;
  81. selectVal=selected_val;
  82. }
  83. $selectTextDom.text(selectText);
  84. $selectValDom.val(selectVal);
  85. if(selectText==""){
  86. $selectTextDom.text("請選擇");
  87. $selectTextDom[0].setAttribute("data-is-select","false");
  88. }else{
  89. $selectTextDom[0].setAttribute("data-is-select","true");
  90. }
  91. })
  92. </script>
  93. </head>
  94. <body>
  95. <div class="dropup" style="position: relative;">
  96. <button class="btn btn-default dropdown-toggle form-control select_multiple" style="width: 20%;margin-left: 0px;" type="button" id="dropdownMenu21" data-toggle="dropdown">
  97. <span class="select_text" data-is-select="false">Dro pup</span>
  98. <span class="caret"></span>
  99. </button>
  100. <ul class="dropdown-menu dropdown_item" style="bottom: auto;">
  101. <li><input type="checkbox" class="check_box" value="aa" /> <span>Action</span></li>
  102. <li><input type="checkbox" class="check_box" value="bb"/> <span>Another action</span></li>
  103. <li><input type="checkbox" class="check_box" value="cc"/> <span>Something else here</span></li>
  104. <li><input type="checkbox" class="check_box" value="dd"/> <span>Separated link</span></li>
  105. </ul><!-- 為了方便演示,type設定text了,實際中可以設定成hidden -->
  106. <input type="hidden" name="" class="select_val"/>
  107. </div>
  108. <select>
  109. <!--<option>-->
  110. <!--<ul class="dropdown-menu dropdown_item" style="bottom: auto;">-->
  111. <!--<li><input type="checkbox" class="check_box" value="aa" /> <span>Action</span></li>-->
  112. <!--<li><input type="checkbox" class="check_box" value="bb"/> <span>Another action</span></li>-->
  113. <!--<li><input type="checkbox" class="check_box" value="cc"/> <span>Something else here</span></li>-->
  114. <!--<li><input type="checkbox" class="check_box" value="dd"/> <span>Separated link</span></li>-->
  115. <!--</ul>&lt;!&ndash; 為了方便演示,type設定text了,實際中可以設定成hidden &ndash;&gt;-->
  116. <!--</option>-->
  117. <option>red</option>
  118. </select>
  119. </body>
  120. </html>