Use JavaScript to set the upper and lower order of items in ListBox

Source: Internet
Author: User
Today, when rewriting the sorting Operation Method in the background management, I wrote a script program that runs on the client to change the order of items in the ListBox control, now, I want to help JavaScript beginners. Of course, we also recommend that you do not joke about JavaScript.
Note the following before you post the Code:

  • Normally, when you use this function, it is meaningless to adjust the order of items in the ListBox on the client. (Of course, if you use Ajax technology to save the order, that's another question), because the order of items adjusted using the client's JavaScript, When you click a new button, the Order will return to the status when the page was initially loaded, not just the order of items, when you use JavaScript to dynamically add (delete) items in ListBox, the status cannot be changed after the API is sent back. This is what you use. net does not work with the server-side control ListBox. (Of course, dropdownlist does not work). Therefore, it is a little more difficult to save the changes to the status than other controls. I usually. in the. NET environment, the textbox (display: None) on the two servers are used to record the text and value of items in the ListBox in sequence in the form of delimiters. Of course, when do you need to record it? You can re-write the values of the two textbox every time when The ListBox item is changed on the client, you can also rewrite the values of the two textbox items on the client before saving them. After the client-side JavaScript changes its value to the textbox on the server side, the changed value can be saved when it is sent back to the server, in this way, you can obtain the text and value of all items in the ListBox separated by delimiters from the text attribute of the two textbox on the server side, this solves the problem of accurately obtaining The ListBox status on the server. Of course, another problem is that after the page is re-loaded, The ListBox returns to the status when the page is opened, and there is a difference in display, therefore, you need to add a Javascript script at the end of the page. The function to be implemented is to determine whether the values of the two textbox are cleared first if they are not empty, then, the split method is used to separate the values of the two Textbox (one is text and the other is value), and the loop is added to the ListBox. This solves all the problems during the running process, the status of controls such as ListBox or dropdownlist is saved accurately!
  • You need to use the setTimeout and cleartimeout methods properly to keep up (down) Operations uninterrupted after clicking the mouse. For details, refer to the code.
  1. <! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
  2. <HTML>
  3. <Head>
  4. <Title> sorting </title>
  5. <SCRIPT>
  6. VaR x = NULL;
  7. VaR listobj = NULL;
  8. // The operation when the mouse is pressed
  9. Function settimestart (type)
  10. {
  11. Listobj = Document. getelementbyid ('forder ');
  12. // Initiate continuous up (down) operations over 0.3 seconds
  13. If (type = "up ")
  14. {
  15. X = setTimeout (uplistitem, 300 );
  16. } Else
  17. {
  18. X = setTimeout (downlistitem, 300 );
  19. }
  20. }
  21. // Move the selected item up
  22. Function uplistitem ()
  23. {
  24. VaR selindex = listobj. selectedindex;
  25. If (selindex <0)
  26. {
  27. If (X! = NULL) {cleartimeout (x );}
  28. Return;
  29. }
  30. If (selindex = 0)
  31. {
  32. If (X! = NULL) {cleartimeout (x );}
  33. Return;
  34. }
  35. VaR selvalue = listobj. Options [selindex]. value;
  36. VaR seltext = listobj. Options [selindex]. text;
  37. Listobj. Options [selindex]. value = listobj. Options [selIndex-1]. value;
  38. Listobj. Options [selindex]. Text = listobj. Options [selIndex-1]. text;
  39. Listobj. Options [selIndex-1]. value = selvalue;
  40. Listobj. Options [selIndex-1]. Text = seltext;
  41. Listobj. selectedindex = selIndex-1;
  42. If (selindex + 1> 0)
  43. {
  44. X = setTimeout (uplistitem, 200)
  45. }
  46. }
  47. // Drop the selected item down
  48. Function downlistitem ()
  49. {
  50. VaR selindex = listobj. selectedindex;
  51. If (selindex <0)
  52. {
  53. If (X! = NULL) {cleartimeout (x );}
  54. Return;
  55. }
  56. If (selindex = listobj. Options. Length-1)
  57. {
  58. If (X! = NULL) {cleartimeout (x );}
  59. Return;
  60. }
  61. VaR selvalue = listobj. Options [selindex]. value;
  62. VaR seltext = listobj. Options [selindex]. text;
  63. Listobj. Options [selindex]. value = listobj. Options [selindex + 1]. value;
  64. Listobj. Options [selindex]. Text = listobj. Options [selindex + 1]. text;
  65. Listobj. Options [selindex + 1]. value = selvalue;
  66. Listobj. Options [selindex + 1]. Text = seltext;
  67. Listobj. selectedindex = selindex + 1;
  68. If (selindex + 1 <listobj. Options. Length-1)
  69. {
  70. X = setTimeout (downlistitem, 200)
  71. }
  72. }
  73. </SCRIPT>
  74. </Head>
  75. <Body topmargin = "20px" leftmargin = "10px" rightmargin = "0">
  76. <Table id = "Table1" Height = "100%" cellspacing = "0" cellpadding = "0" width = "100%" border = "0">
  77. <Tr>
  78. <TD valign = "TOP">
  79. <Table id = "table4" cellspacing = "0" cellpadding = "0" width = "100%" border = "0">
  80. <Tr>
  81. <TD> <input class = "upbtn" type = "button" value = "↑ "onmousedown = "settimestart ('up');" onmouseup = "cleartimeout (X );"
  82. Onclick = "listobj = document. getelementbyid ('forder'); uplistitem (); cleartimeout (x); "id =" button1 "name =" button1 "> <input style =" width: 48px; height: 22px "type =" button "value =" lag down "class =" downbtn "onmousedown =" settimestart ('drop ');"
  83. Onmouseup = "cleartimeout (x);" onclick = "listobj = document. getelementbyid ('forder'); downlistitem (); cleartimeout (x); "id =" button2 "name =" button2 ">
  84. </TD>
  85. </Tr>
  86. <Tr>
  87. <TD>
  88. <Select id = "forder" style = "width: 304px; Height: 240px" size = "15">
  89. <Option value = 1> 1 </option>
  90. <Option value = 2> 2 </option>
  91. <Option value = 3> 3 </option>
  92. <Option value = 4> 4 </option>
  93. <Option value = 5> 5 </option>
  94. <Option value = 6> 6 </option>
  95. <Option value = 7> 7 </option>
  96. <Option value = 8> 8 </option>
  97. <Option value = 9> 9 </option>
  98. </SELECT> </TD>
  99. </Tr>
  100. </Table>
  101. </TD>
  102. </Tr>
  103. </Table>
  104. </Body>
  105. </Html>

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.