Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [JavaScript] Formularz z Checkboxami - kolorowe tła
coder()
post
Post #1





Grupa: Zarejestrowani
Postów: 21
Pomógł: 0
Dołączył: 3.05.2007

Ostrzeżenie: (0%)
-----


Witam.
Napisałem dzisiaj taki formularz z załączonym skryptem JS. Całość działa swietnie i wogóle... Ale jest całość jest troche duża co przy połączeniu z php i dynamicznym generowaniu dużych ilosci wierszy formularza może znacznie zmulić serwer i przeglądarke klienta. Czy byłby ktoś w stanie to jakos zminimalizować?? Bo ja próbuje i co zmienie jakąś nazwe/id/klase itp to wszystko sie wali (IMG:http://forum.php.pl/style_emoticons/default/sad.gif)

(całość ma przypominać w działaniu panel PhpMyAdmin)
Oto kod:
  1. <script type="text/javascript">
  2. function getElementsByClassName(cn){
  3. var arr = new Array();
  4. var els = document.getElementsByTagName("*");
  5. var exp= new RegExp("^(.* )?"+cn+"( .*)?$", "g");
  6. for (var i = 0; i < els.length; i++ ){
  7. if (exp.test(els[i].className)){
  8. arr.push(els[i]);
  9. }
  10. }
  11. return arr;
  12. }
  13.  
  14. function select_all(obj_check, cn_check, cn_uncheck, num_rows, style) {
  15. var checkboxes = getElementsByClassName(cn_check);
  16. var un_checkboxes = getElementsByClassName(cn_uncheck);
  17. for (i = 0; i < checkboxes.length; i++) {
  18. if (obj_check.checked == true) {
  19. checkboxes[i].checked = true;
  20. for (x = 0; x < un_checkboxes.length; x++) {
  21. un_checkboxes[x].checked = false;
  22. }
  23. } else {
  24. checkboxes[i].checked = false;
  25. }
  26. }
  27. for (i = 0; i < num_rows; i++) {
  28. if (obj_check.checked) {
  29. document.getElementById('accept' + i).style.background=style;
  30. document.getElementById('locked' + i).style.background=style;
  31. document.getElementById('phrase' + i).style.background=style;
  32. document.getElementById('phrase_row' + i).style.background=style;
  33. } else if (obj_check.checked == false && checkboxes[i].checked == false && un_checkboxes[i].checked == false) {
  34. document.getElementById('accept' + i).style.background='';
  35. document.getElementById('locked' + i).style.background='';
  36. document.getElementById('phrase' + i).style.background='';
  37. document.getElementById('phrase_row' + i).style.background='';
  38. }
  39. }
  40. }
  41. function select_all2(obj_check, cn_check, cn_uncheck, num_rows, style) {
  42. obj_check.checked = !obj_check.checked;
  43. select_all(obj_check, cn_check, cn_uncheck);
  44. var checkboxes = getElementsByClassName(cn_check);
  45. var un_checkboxes = getElementsByClassName(cn_uncheck);
  46. for (i = 0; i < num_rows; i++) {
  47. if (obj_check.checked) {
  48. document.getElementById('accept' + i).style.background=style;
  49. document.getElementById('locked' + i).style.background=style;
  50. document.getElementById('phrase' + i).style.background=style;
  51. document.getElementById('phrase_row' + i).style.background=style;
  52. } else if (obj_check.checked == false && checkboxes[i].checked == false && un_checkboxes[i].checked == false) {
  53. document.getElementById('accept' + i).style.background='';
  54. document.getElementById('locked' + i).style.background='';
  55. document.getElementById('phrase' + i).style.background='';
  56. document.getElementById('phrase_row' + i).style.background='';
  57. }
  58. }
  59. }
  60.  
  61. function select_checkbox(obj_checked, uncheck_name, style, id){
  62. var checkbox = document.getElementsByName(uncheck_name)
  63. if (obj_checked.checked == true) {
  64. document.getElementById('accept' + id).style.background=style;
  65. document.getElementById('locked' + id).style.background=style;
  66. document.getElementById('phrase' + id).style.background=style;
  67. document.getElementById('phrase_row' + id).style.background=style;
  68. checkbox[0].checked = false;
  69. }
  70. }
  71.  
  72. function changeBox(cbox, ucbox, style, id) {
  73. ubox = eval(ucbox);
  74. document.getElementById('accept' + id).style.background=style;
  75. document.getElementById('locked' + id).style.background=style;
  76. document.getElementById('phrase' + id).style.background=style;
  77. document.getElementById('phrase_row' + id).style.background=style;
  78. if (ubox.checked) {
  79. ubox.checked = !ubox.checked;
  80. box = eval(cbox);
  81. box.checked = !box.checked;
  82. } else {
  83. box = eval(cbox);
  84. box.checked = !box.checked;
  85. }
  86. }
  87.  
  88. function bg_phrase(id, style){
  89. document.getElementById(id).style.background=style;
  90. }
  91.  
  92. function bg_phrase_out(id, style){
  93. document.getElementById(id).style.background=style;
  94. }
  95.  
  96. function bg_phrase2(kolumna, id, style, box_checked, box_uchecked, td_checked){
  97. var box1 = document.getElementById(box_checked);
  98. var box2 = document.getElementById(box_uchecked);
  99. if (box1.checked == false && box2.checked == false) {
  100. document.getElementById('accept' + id).style.background=style;
  101. document.getElementById('locked' + id).style.background=style;
  102. document.getElementById('phrase' + id).style.background=style;
  103. document.getElementById('phrase_row' + id).style.background=style;
  104. } else if (box1.checked == true && box2.checked == false && kolumna == 'locked') {
  105. document.getElementById(kolumna + id).style.background=style;
  106. } else if (box1.checked == false && box2.checked == true && (kolumna == 'accept' || kolumna == 'phrase')) {
  107. document.getElementById('accept' + id).style.background=style;
  108. }
  109. }
  110.  
  111. function bg_phrase_out2(kolumna, id, style, box_checked, box_uchecked){
  112. var box1 = document.getElementById(box_checked);
  113. var box2 = document.getElementById(box_uchecked);
  114. var kolor = document.getElementById('phrase_row' + id).style.background;
  115. if (box1.checked == false && box2.checked == false) {
  116. document.getElementById('accept' + id).style.background='';
  117. document.getElementById('locked' + id).style.background='';
  118. document.getElementById('phrase' + id).style.background='';
  119. document.getElementById('phrase_row' + id).style.background='';
  120. } else if (box1.checked == true && box2.checked == false && kolumna == 'locked') {
  121. document.getElementById(kolumna + id).style.background=kolor;
  122. } else if (box1.checked == false && box2.checked == true && (kolumna == 'accept' || kolumna == 'phrase')) {
  123. document.getElementById('accept' + id).style.background=kolor;
  124. }
  125. }
  126. </head>


  1. <tr id="phrase_row0">
  2. <td class="accept" id="accept0"
  3. onMouseOver="bg_phrase2('accept', '0', '#CCFFCC', 'a_checkbox0', 'b_checkbox0', 'phraset0');"
  4. onMouseOut="bg_phrase_out2('accept', '0', '#FFFFFF', 'a_checkbox0', 'b_checkbox0');">
  5. <input class="a_checkbox" id="a_checkbox0"
  6. onclick="select_checkbox(this, 'x0', '#CCFFCC', '0');" type="checkbox" name="y0" value="opcja1" /></td>
  7. <td class="locked" id="locked0"
  8. onMouseOver="bg_phrase2('locked', '0', '#FFCCCC', 'a_checkbox0', 'b_checkbox0', 'phraset0');"
  9. onMouseOut="bg_phrase_out2('locked', '0', '#FFFFFF', 'a_checkbox0', 'b_checkbox0');">
  10. <input class="b_checkbox" id="b_checkbox0"
  11. onclick="select_checkbox(this, 'y0', '#FFCCCC', '0');" type="checkbox" name="x0" value="opcja1" /></td>
  12. <td class="phrase" id="phrase0"
  13. onMouseOver="bg_phrase2('phrase', '0', '#CCFFCC', 'a_checkbox0', 'b_checkbox0', 'phraset0');"
  14. onMouseOut="bg_phrase_out2('phrase', '0', '#FFFFFF', 'a_checkbox0', 'b_checkbox0');"
  15. onclick="changeBox(document.getElementById('a_checkbox0'), document.getElementById('b_checkbox0'), '#CCFFCC', '0');">
  16. Opcja 1<br /></td>
  17. </tr>
  18. <tr id="phrase_row1">
  19. <td class="accept" id="accept1"
  20. onMouseOver="bg_phrase2('accept', '1', '#CCFFCC', 'a_checkbox1', 'b_checkbox1', 'phraset1');"
  21. onMouseOut="bg_phrase_out2('accept', '1', '#FFFFFF', 'a_checkbox1', 'b_checkbox1');">
  22. <input class="a_checkbox" id="a_checkbox1"
  23. onclick="select_checkbox(this, 'x1', '#CCFFCC', '1');" type="checkbox" name="y1" value="opcja2" /></td>
  24. <td class="locked" id="locked1"
  25. onMouseOver="bg_phrase2('locked', '1', '#FFCCCC', 'a_checkbox1', 'b_checkbox1', 'phraset1');"
  26. onMouseOut="bg_phrase_out2('locked', '1', '#FFFFFF', 'a_checkbox1', 'b_checkbox1');">
  27. <input class="b_checkbox" id="b_checkbox1"
  28. onclick="select_checkbox(this, 'y1', '#FFCCCC', '1');" type="checkbox" name="x1" value="opcja2" /></td>
  29. <td class="phrase" id="phrase1"
  30. onMouseOver="bg_phrase2('phrase', '1', '#CCFFCC', 'a_checkbox1', 'b_checkbox1', 'phraset1');"
  31. onMouseOut="bg_phrase_out2('phrase', '1', '#FFFFFF', 'a_checkbox1', 'b_checkbox1');"
  32. onclick="changeBox(document.getElementById('a_checkbox1'), document.getElementById('b_checkbox1'), '#CCFFCC', '1');">
  33. Opcja 2<br /></td>
  34. </tr>
  35. <tr id="phrase_row2">
  36. <td class="accept" id="accept2"
  37. onMouseOver="bg_phrase2('accept', '2', '#CCFFCC', 'a_checkbox2', 'b_checkbox2', 'phraset2');"
  38. onMouseOut="bg_phrase_out2('accept', '2', '#FFFFFF', 'a_checkbox2', 'b_checkbox2');">
  39. <input class="a_checkbox" id="a_checkbox2"
  40. onclick="select_checkbox(this, 'x2', '#CCFFCC', '2');" type="checkbox" name="y2" value="opcja3" /></td>
  41. <td class="locked" id="locked2"
  42. onMouseOver="bg_phrase2('locked', '2', '#FFCCCC', 'a_checkbox2', 'b_checkbox2', 'phraset2');"
  43. onMouseOut="bg_phrase_out2('locked', '2', '#FFFFFF', 'a_checkbox2', 'b_checkbox2');">
  44. <input class="b_checkbox" id="b_checkbox2"
  45. onclick="select_checkbox(this, 'y2', '#FFCCCC', '2');" type="checkbox" name="x2" value="opcja3" /></td>
  46. <td class="phrase" id="phrase2"
  47. onMouseOver="bg_phrase2('phrase', '2', '#CCFFCC', 'a_checkbox2', 'b_checkbox2', 'phraset2');"
  48. onMouseOut="bg_phrase_out2('phrase', '2', '#FFFFFF', 'a_checkbox2', 'b_checkbox2');"
  49. onclick="changeBox(document.getElementById('a_checkbox2'), document.getElementById('b_checkbox2'), '#CCFFCC', '2');">
  50. Opcja 3<br /></td>
  51. </tr>
  52. <tr id="phrase_row3">
  53. <td class="accept" id="accept3"
  54. onMouseOver="bg_phrase2('accept', '3', '#CCFFCC', 'a_checkbox3', 'b_checkbox3', 'phraset3');"
  55. onMouseOut="bg_phrase_out2('accept', '3', '#FFFFFF', 'a_checkbox3', 'b_checkbox3');">
  56. <input class="a_checkbox" id="a_checkbox3"
  57. onclick="select_checkbox(this, 'x3', '#CCFFCC', '3');" type="checkbox" name="y3" value="opcja4" /></td>
  58. <td class="locked" id="locked3"
  59. onMouseOver="bg_phrase2('locked', '3', '#FFCCCC', 'a_checkbox3', 'b_checkbox3', 'phraset3');"
  60. onMouseOut="bg_phrase_out2('locked', '3', '#FFFFFF', 'a_checkbox3', 'b_checkbox3');">
  61. <input class="b_checkbox" id="b_checkbox3"
  62. onclick="select_checkbox(this, 'y3', '#FFCCCC', '3');" type="checkbox" name="x3" value="opcja4" /></td>
  63. <td class="phrase" id="phrase3"
  64. onMouseOver="bg_phrase2('phrase', '3', '#CCFFCC', 'a_checkbox3', 'b_checkbox3', 'phraset3');"
  65. onMouseOut="bg_phrase_out2('phrase', '3', '#FFFFFF', 'a_checkbox3', 'b_checkbox3');"
  66. onclick="changeBox(document.getElementById('a_checkbox3'), document.getElementById('b_checkbox3'), '#CCFFCC', '3');">
  67. Opcja 4<br /><br /></td>
  68. </tr>
  69. <tr id="phraseall">
  70. <td class="accept" id="phrase_a"
  71. onMouseOver="bg_phrase('phraseall', '#CCFFCC');"
  72. onMouseOut="bg_phrase_out('phraseall', '#FFFFFF');">
  73. <input type="checkbox" class="a_checkbox" id="a_select_all" name="select_all[]" value="select_all"
  74. onclick="select_all(this, 'a_checkbox', 'b_checkbox', '4', '#CCFFCC');" /></td>
  75. <td class="locked" id="phrase_l"
  76. onMouseOver="bg_phrase('phraseall', '#FFCCCC');"
  77. onMouseOut="bg_phrase_out('phraseall', '#FFFFFF');">
  78. <input type="checkbox" class="b_checkbox" id="b_select_all" name="select_all[]" value="select_all"
  79. onclick="select_all(this, 'b_checkbox', 'a_checkbox', '4', '#FFCCCC');" /></td>
  80. <td class="phrase" id="phrase_p"
  81. onMouseOver="bg_phrase('phraseall', '#CCFFCC');"
  82. onMouseOut="bg_phrase_out('phraseall', '#FFFFFF');"
  83. onclick="select_all2(document.getElementById('a_select_all'), 'a_checkbox', 'b_checkbox', '4', '#CCFFCC');">
  84. Zaznacz wszystko</td>
  85. </tr>
  86. </form>
  87. </body>
  88. </html>
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
nospor
post
Post #2





Grupa: Moderatorzy
Postów: 36 559
Pomógł: 6315
Dołączył: 27.12.2004




Pierwsza rzecz jaka mi sie rzuca w oczy:
Kod
checkboxes = getElementsByClassName(cn_check);

No a twoja funkcja getElementsByClassName() pobiera wszystkie elementy na stronie (div, a, inne ) i wnich szuka danej klasy. Kiepsciutko. Skoro szukasz checkboxow, to powinienes pobrac tylko inputy i to najlepiej z type=checkbox i dopiero w nich szukac swojej klasy.

Reszty mi sie niechce analizowac bo uwazam ze nie ma to sensu. Zainteresuj sie gotowymi libami, jak np jQuery, ktore odwali za Ciebie większą czesc kodu co tu pokazales. Na dodatek bedziesz mial pewnosc ze pod kazda przegladarka bedzie ci dzialac i ze bedzie dzialac ci szybciej niz twoj kod
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 3.10.2025 - 18:18