良いFormマルチ選択リストコントロール効果

55602 ワード

多くの人がformをデフォルトにしています
選択された項目と選択されていない項目が一緒であるため、複数選択ボックスの効果はあまり満足していません.選択項目が多い場合、特に問題があります.ここでは、選択可能な項目と選択項目を分離し、ボタンを操作したりダブルクリックしたりしてオプションを移動することができ、非常に便利です.
まず、次のコードをページのラベルに配置します.
CSS:

   
   
   
   
  1.  
  2. <style type= "text/css">
  3.  
  4.  
  5. .multipleSelectBoxControl span { /* Labels above select boxes*/
  6. font-family:arial;
  7. font-size:11px;
  8. font-weight: bold;
  9. }
  10. .multipleSelectBoxControl div select { /* Select box layout */
  11. font-family:arial;
  12. height: 100%;
  13. }
  14. .multipleSelectBoxControl input { /* Small butons */
  15. width:25px;
  16. }
  17. .multipleSelectBoxControl div {
  18. float: left;
  19. }
  20. .multipleSelectBoxDiv
  21. </style>
  22.  

Javascript:

   
   
   
   
  1.  
  2. <script type= "text/javascript">
  3. /************************************************************************************************************
  4. (C) www.dhtmlgoodies.com, October 2005
  5. This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.
  6. Terms of use:
  7. You are free to use this script as long as the copyright message is kept intact. However, you may not
  8. redistribute, sell or repost it without our permission.
  9. Thank you!
  10. www.dhtmlgoodies.com
  11. Alf Magne Kalleland
  12. ************************************************************************************************************/
  13. var fromBoxArray = new Array ( );
  14. var toBoxArray = new Array ( );
  15. var selectBoxIndex = 0;
  16. function moveSingleElement ( )
  17. {
  18. var selectBoxIndex = this. parentNode. parentNode. id. replace ( /[^/d]/g, '' );
  19. var tmpFromBox;
  20. var tmpToBox;
  21. if ( this. tagName. toLowerCase ( )== 'select' ) {
  22. tmpFromBox = this;
  23. if (tmpFromBox==fromBoxArray [selectBoxIndex ] )tmpToBox = toBoxArray [selectBoxIndex ]; else tmpToBox = fromBoxArray [selectBoxIndex ];
  24. } else {
  25. if ( this. value. indexOf ( '>' )>= 0 ) {
  26. tmpFromBox = fromBoxArray [selectBoxIndex ];
  27. tmpToBox = toBoxArray [selectBoxIndex ];
  28. } else {
  29. tmpFromBox = toBoxArray [selectBoxIndex ];
  30. tmpToBox = fromBoxArray [selectBoxIndex ];
  31. }
  32. }
  33. for ( var no= 0;no<tmpFromBox. options. length;no++ ) {
  34. if (tmpFromBox. options [no ]. selected ) {
  35. tmpFromBox. options [no ]. selected = false;
  36. tmpToBox. options [tmpToBox. options. length ] = new Option (tmpFromBox. options [no ]. text,tmpFromBox. options [no ]. value );
  37. for ( var no2=no;no2< (tmpFromBox. options. length -1 );no2++ ) {
  38. tmpFromBox. options [no2 ]. value = tmpFromBox. options [no2 +1 ]. value;
  39. tmpFromBox. options [no2 ]. text = tmpFromBox. options [no2 +1 ]. text;
  40. tmpFromBox. options [no2 ]. selected = tmpFromBox. options [no2 +1 ]. selected;
  41. }
  42. no = no -1;
  43. tmpFromBox. options. length = tmpFromBox. options. length -1;
  44. }
  45. }
  46. var tmpTextArray = new Array ( );
  47. for ( var no= 0;no<tmpFromBox. options. length;no++ ) {
  48. tmpTextArray. push (tmpFromBox. options [no ]. text + '___' + tmpFromBox. options [no ]. value );
  49. }
  50. tmpTextArray. sort ( );
  51. var tmpTextArray2 = new Array ( );
  52. for ( var no= 0;no<tmpToBox. options. length;no++ ) {
  53. tmpTextArray2. push (tmpToBox. options [no ]. text + '___' + tmpToBox. options [no ]. value );
  54. }
  55. tmpTextArray2. sort ( );
  56. for ( var no= 0;no<tmpTextArray. length;no++ ) {
  57. var items = tmpTextArray [no ]. split ( '___' );
  58. tmpFromBox. options [no ] = new Option (items [ 0 ],items [ 1 ] );
  59. }
  60. for ( var no= 0;no<tmpTextArray2. length;no++ ) {
  61. var items = tmpTextArray2 [no ]. split ( '___' );
  62. tmpToBox. options [no ] = new Option (items [ 0 ],items [ 1 ] );
  63. }
  64. }
  65. function moveAllElements ( )
  66. {
  67. var selectBoxIndex = this. parentNode. parentNode. id. replace ( /[^/d]/g, '' );
  68. var tmpFromBox;
  69. var tmpToBox;
  70. if ( this. value. indexOf ( '>' )>= 0 ) {
  71. tmpFromBox = fromBoxArray [selectBoxIndex ];
  72. tmpToBox = toBoxArray [selectBoxIndex ];
  73. } else {
  74. tmpFromBox = toBoxArray [selectBoxIndex ];
  75. tmpToBox = fromBoxArray [selectBoxIndex ];
  76. }
  77. for ( var no= 0;no<tmpFromBox. options. length;no++ ) {
  78. tmpToBox. options [tmpToBox. options. length ] = new Option (tmpFromBox. options [no ]. text,tmpFromBox. options [no ]. value );
  79. }
  80. tmpFromBox. options. length= 0;
  81. }
  82. function createMovableOptions (fromBox,toBox,totalWidth,totalHeight,labelLeft,labelRight )
  83. {
  84. fromObj = document. getElementById (fromBox );
  85. toObj = document. getElementById (toBox );
  86.  
  87. fromObj. ondblclick = moveSingleElement;
  88. toObj. ondblclick = moveSingleElement;
  89.  
  90. fromBoxArray. push (fromObj );
  91. toBoxArray. push (toObj );
  92. var parentEl = fromObj. parentNode;
  93. var parentDiv = document. createElement ( 'DIV' );
  94. parentDiv. className= 'multipleSelectBoxControl';
  95. parentDiv. id = 'selectBoxGroup' + selectBoxIndex;
  96. parentDiv. style. width = totalWidth + 'px';
  97. parentDiv. style. height = totalHeight + 'px';
  98. parentEl. insertBefore (parentDiv,fromObj );
  99. var subDiv = document. createElement ( 'DIV' );
  100. subDiv. style. width = (Math. floor (totalWidth/ 2 ) - 15 ) + 'px';
  101. fromObj. style. width = (Math. floor (totalWidth/ 2 ) - 15 ) + 'px';
  102.  
  103. var label = document. createElement ( 'SPAN' );
  104. label. innerHTML = labelLeft;
  105. subDiv. appendChild (label );
  106. subDiv. appendChild (fromObj );
  107. subDiv. className = 'multipleSelectBoxDiv';
  108. parentDiv. appendChild (subDiv );
  109. var buttonDiv = document. createElement ( 'DIV' );
  110. buttonDiv. style. verticalAlign = 'middle';
  111. buttonDiv. style. paddingTop = (totalHeight/ 2 ) - 50 + 'px';
  112. buttonDiv. style. width = '30px';
  113. buttonDiv. style. textAlign = 'center';
  114. parentDiv. appendChild (buttonDiv );
  115. var buttonRight = document. createElement ( 'INPUT' );
  116. buttonRight. type= 'button';
  117. buttonRight. value = '>';
  118. buttonDiv. appendChild (buttonRight );
  119. buttonRight. onclick = moveSingleElement;
  120. var buttonAllRight = document. createElement ( 'INPUT' );
  121. buttonAllRight. type= 'button';
  122. buttonAllRight. value = '>>';
  123. buttonAllRight. onclick = moveAllElements;
  124. buttonDiv. appendChild (buttonAllRight );
  125. var buttonLeft = document. createElement ( 'INPUT' );
  126. buttonLeft. style. marginTop= '10px';
  127. buttonLeft. type= 'button';
  128. buttonLeft. value = '<';
  129. buttonLeft. onclick = moveSingleElement;
  130. buttonDiv. appendChild (buttonLeft );
  131. var buttonAllLeft = document. createElement ( 'INPUT' );
  132. buttonAllLeft. type= 'button';
  133. buttonAllLeft. value = '<<';
  134. buttonAllLeft. onclick = moveAllElements;
  135. buttonDiv. appendChild (buttonAllLeft );
  136. var subDiv = document. createElement ( 'DIV' );
  137. subDiv. style. width = (Math. floor (totalWidth/ 2 ) - 15 ) + 'px';
  138. toObj. style. width = (Math. floor (totalWidth/ 2 ) - 15 ) + 'px';
  139.  
  140. var label = document. createElement ( 'SPAN' );
  141. label. innerHTML = labelRight;
  142. subDiv. appendChild (label );
  143. subDiv. appendChild (toObj );
  144. parentDiv. appendChild (subDiv );
  145. toObj. style. height = (totalHeight - label. offsetHeight ) + 'px';
  146. fromObj. style. height = (totalHeight - label. offsetHeight ) + 'px';
  147.  
  148. selectBoxIndex++;
  149. }
  150. </script>
  151.  

次に、次のコードをページの「body」タブに配置します.
HTML:

   
   
   
   
  1.  
  2. <form method="post" action="multiple_select.html">
  3. <select multiple name="fromBox" id="fromBox">
  4. <option value="3">Finland </option>
  5. <option value="4">France </option>
  6. <option value="6">Mexico </option>
  7. <option value="1">Norway </option>
  8. <option value="5">Spain </option>
  9. <option value="2">United Kingdom </option>
  10. </select>
  11. <select multiple name="toBox" id="toBox">
  12. <option value="12">Canada </option>
  13. <option value="13">Germany </option>
  14. <option value="11">United States </option>
  15. </select>
  16. </form>
  17. <script type="text/javascript">
  18. createMovableOptions("fromBox","toBox",500,300,'Available countries','Selected countries');
  19. </script>
  20. <p>You move elements by clicking on the buttons or by double clicking on select box items </p>
  21.  

構成方法:
(1)次のようなコードで2つ作成
複数選択リスト
HTML:

   
   
   
   
  1.  
  2. <select multiple name="fromBox" id="fromBox">
  3. <option value="3">Finland </option>
  4. <option value="4">France </option>
  5. <option value="6">Mexico </option>
  6. <option value="1">Norway </option>
  7. <option value="5">Spain </option>
  8. <option value="2">United Kingdom </option>
  9. </select>
  10. <select multiple name="toBox[]" id="topBox">
  11. <option value="12">Canada </option>
  12. <option value="13">Germany </option>
  13. <option value="11">United States </option>
  14. </select>
  15.  

(2)次のjavascriptコードで設定を初期化します.
Javascript:

   
   
   
   
  1.  
  2. <script type= "text/javascript">
  3. createMovableOptions ( "fromBox", "toBox", 500, 300, 'Available countries', 'Selected countries' );
  4. </script>
  5.  

パラメータの説明は次のとおりです.
      
「fromBox」-最初の
複数選択リストのid()
      
500-コントロールの合計幅
      
300-コントロールの合計高さ
      
「Available countries」-最初の
複数選択リストの表示名
      
「Selected countries」-2番目
複数選択リストの表示名
フォームの発行方法:
フォームをコミットする場合は、選択したオプション値のみがコミットされることを望んでいるので、javascriptコードの小さな処理が必要です.
まずformタグにコミットイベントの処理を追加します.
Code:

javascriptコードを追加します.
Javascript:

   
   
   
   
  1.  
  2. function selectItem ( )
  3. {
  4. var obj = document. getElementById ( 'toBox[]' );
  5. for ( var no= 0;no<obj. options. length;no++ ) {
  6. obj. options [no ]. selected = true;
  7. }
  8. }
  9.