Multiple-choice list box implemented by JavaScript

Source: Internet
Author: User

  1. <Script language = "JavaScript">
  2. Function mm_findobj (n, d) {// v4.0
  3. VaR P, I, X; If (! D) d = document; if (P = n. indexof ("? ")> 0 & parent. frames. Length ){
  4. D=parent.frames[n.substring(p%1%].doc ument; n = n. substring (0, P );}
  5. If (! (X = d [N]) & D. All) x = D. All [N]; for (I = 0 ;! X & I <D. Forms. length; I ++) x = D. Forms [I] [N];
  6. For (I = 0 ;! X & D. Layers & I <D. layers. length; I ++) xw.mm_findobj(n,d.layers+ I }.doc ument );
  7. If (! X & document. getelementbyid) x = Document. getelementbyid (n); Return X;
  8. }
  9. Function checkposttype (DES, OP ){
  10. If (! Des |! OP) return true;
  11. VaR flag = true;
  12. VaR Ops = des. options;
  13. VaR I = 0;
  14. For (I = 0; I <ops. length; I ++ ){
  15. If (Ops. Item (I). value = op. Value ){
  16. Flag = false;
  17. }
  18. If (Ops. Item (I). Value % 100 = 0 & ops. Item (I). value = op. value-op.value % 100 ){
  19. Flag = false;
  20. }
  21. If (! Flag ){
  22. Alert ('option ['+ ops. Item (I). innertext +'] already contains ['+ OP. innertext +']. ');
  23. Return flag;
  24. }
  25. If (op. Value % 100 = 0 & OP. value = ops. Item (I). value-ops.item (I). Value % 100 ){
  26. Ops. Remove (I );
  27. I --;
  28. }
  29. }
  30. Return flag;
  31. }
  32. // This function allows you to delete and move selected items from the list to another list. 2001/04/04
  33. // Parameter table:
  34. // SRC Source list
  35. // List of des targets (if omitted, the items selected in the source list will be deleted)
  36. // Exce if the text of an item in the source list is the same as this parameter, the item cannot be removed or deleted (can be omitted)
  37. // D1 specifies the framework or window in which the source list is located (if omitted, search in the current framework or window)
  38. // D2 specifies the frame or window in which the target list is located (if omitted, search in the current frame or window)
  39. //
  40. Function selmove (SRC, Des, check, exce, D1, D2, del) {If (! SRC) return false;
  41. VaR osrc = mm_findobj (SRC, D1 );
  42. If (! Osrc) return false;
  43. If (! Des ){
  44. For (I = 0; I <osrc. Options. length; I ++ ){
  45. If (osrc. Options (I). Selected ){
  46. If (osrc. Options (I). innertext! = Exce ){
  47. Osrc. Options. Remove (I );
  48. I --;
  49. }
  50. }
  51. }
  52. }
  53. Else {
  54. VaR odes = mm_findobj (DES, D2 );
  55. If (! ODES) return false;
  56. For (I = 0; I <osrc. Options. length; I ++ ){
  57. VaR osrcoption = osrc. Options. item (I );
  58. If (osrcoption. Selected ){
  59. If (check & check (ODES, osrcoption )){
  60. If (osrcoption. innertext! = Exce ){
  61. VaR ooption = Document. createelement ("option ");
  62. Odes. Options. Add (ooption );
  63. Ooption. innertext = osrcoption. innertext;
  64. Ooption. value = osrcoption. value;
  65. Osrcoption. Selected = false;
  66. If (DEL) {osrc. Options. Remove (I); I --;}
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }
  73. Function check_city (DES, OP ){
  74. If (checkposttype (DES, OP )){
  75. If (DES. Options. length> = 3 ){
  76. Alert ("no more than three! ");
  77. Return false;
  78. }
  79. Else return true;
  80. }
  81. Else {
  82. Return false;
  83. }
  84. }
  85. Function selselectedall (osrc ){
  86. If (! Osrc) return false;
  87. VaR I;
  88. For (I = 0; I <osrc. Options. length; I ++ ){
  89. Osrc. Options. Item (I). Selected = true;
  90. }
  91. }
  92. Function check ()
  93. {
  94. Selselectedall (document. form1. B)
  95. }
  96. </SCRIPT>
  97. <Form ID = "form1" name = "form1" method = "Post" Action = "B. asp">
  98. <Select name = "A" size = "10" multiple = "multiple" id = "A" style = "width: 180px; font-size: 12px;">
  99. <Option value = "1"> 1 </option>
  100. <Option value = "2"> 2 </option>
  101. <Option value = "3"> 3 </option>
  102. <Option value = "4"> 4 </option>
  103. <Option value = "5"> 5 </option>
  104. </SELECT>
  105. <A href = "#" onclick = "selmove ('A', 'B', check_city)"> Add </a>
  106. <Select name = "B" size = "10" multiple = "multiple" id = "B" style = "width: 180px; font-size: 12px;">
  107. </SELECT>
  108. <Input type = "Submit" name = "button" id = "button" value = "Submit" onclick = "check ()"/>
  109. </Form>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.