Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Zaznacz wszystkie (name[], jeden button)
Forum PHP.pl > Forum > Po stronie przeglądarki > JavaScript
8_pasarzer_NOSTROMO
Kto pomoże przerobić kod aby był jeden przycisk (Zaznacz wszystko / Odznacz wszystko)
Podaje kod, działający (IE,Fox,Chrome):

  1. <script LANGUAGE="JavaScript">
  2. function SetValues(Form, CheckBox, Value)
  3. {
  4. var objCheckBoxes = document.forms[Form].elements[CheckBox];
  5. var countCheckBoxes = objCheckBoxes.length;
  6. for(var i = 0; i < countCheckBoxes; i++)
  7. objCheckBoxes[i].checked = Value;
  8. }
  9.  
  10.  
  11. <form name="sports" method="post" action="insert.php">
  12. <table border = "1" cellpadding = "5">
  13. <tr>
  14. <td><input type='checkbox' name=boxes[] value='Baseball' />Basketball</td>
  15. <td><input type='checkbox' name=boxes[] value='Basketball' />Basketball</td>
  16. <td><input type='checkbox' name=boxes[] value='Bowling' />Bowling</td>
  17. </tr><tr>
  18. <td><input type='checkbox' name=boxes[] value='Rugby' />Rugby</td>
  19. <td><input type='checkbox' name=boxes[] value='Tennis' />Tennis</td>
  20. <td><input type='checkbox' name=boxes[] value='Valleyball' />Valleyball</td>
  21. </tr></table>
  22. <input type="button" value="Zaznacz wszystkie" onclick="SetValues('sports', 'boxes[]', true);">
  23. <input type="button" value="Odznacz wszystkie" onclick="SetValues('sports', 'boxes[]', false);">


Prośba o przerobienie na jeden przycisk (Zaznacz/Odznacz)
john_doe
a tak ?
edytowalem bo mi się coś nie udało. teraz działa - sprawdzilem

  1. function SetValues(Form, CheckBox)
  2. {
  3. var objCheckBoxes = document.forms[Form].elements[CheckBox];
  4. var countCheckBoxes = objCheckBoxes.length;
  5. for(var i = 0; i < countCheckBoxes; i++)
  6. {
  7. if(objCheckBoxes[i].checked)
  8. {
  9. //alert('im in');
  10. objCheckBoxes[i].checked = false;
  11.  
  12. }else { objCheckBoxes[i].checked = true; }
  13. }
  14. }
8_pasarzer_NOSTROMO
Dzięki Dżony !

A jeszcze jak przerobić by przycisk był taki dla pustych checkboxów :
  1. <input type="button" value="Zaznacz wszystkie" onclick="SetValues('sports', 'boxes[]', true);">


następnie taki, dla zaznaczonych
  1. <input type="button" value="Odznacz wszystkie" onclick="SetValues('sports', 'boxes[]', true);">


Aby się dynamicznie zmieniał.
john_doe
  1. function SetValues(Form, CheckBox)
  2. {
  3. var objCheckBoxes = document.forms[Form].elements[CheckBox];
  4. var countCheckBoxes = objCheckBoxes.length;
  5. for(var i = 0; i < countCheckBoxes; i++)
  6. {
  7. if(objCheckBoxes[i].checked)
  8. {
  9.  
  10. objCheckBoxes[i].checked = false;
  11.  
  12. document.getElementById("btn").value="Zaznacz wszystkie";
  13.  
  14. }else {
  15. objCheckBoxes[i].checked = true;
  16. document.getElementById("btn").value="Odznacz wszystkie";
  17. }
  18. }
  19. }


ogólnie trzeci parametr wyrzuć z tych inputów jest niepotrzebny już teraz
8_pasarzer_NOSTROMO
Przycisk działa ale, zaznacza tylko pierwszy z inputów i usuwa tylko ten wybrany pierwszy z inputów.

Inputy są w pętli, mniej więcej 30.
john_doe
coś zepsułeś... nie dodałes id do buttona ( id="btn" )

przeklej sobie to wszystko

  1. <script LANGUAGE="JavaScript">
  2.  
  3. function SetValues(Form, CheckBox)
  4. {
  5. var objCheckBoxes = document.forms[Form].elements[CheckBox];
  6. var countCheckBoxes = objCheckBoxes.length;
  7. for(var i = 0; i < countCheckBoxes; i++)
  8. {
  9. if(objCheckBoxes[i].checked)
  10. {
  11.  
  12. objCheckBoxes[i].checked = false;
  13.  
  14. document.getElementById("btn").value="Zaznacz wszystkie";
  15.  
  16. }else {
  17. objCheckBoxes[i].checked = true;
  18. document.getElementById("btn").value="Odznacz wszystkie";
  19. }
  20. }
  21. }
  22.  
  23.  
  24. <form name="sports" method="post" action="insert.php">
  25.  
  26. <input type='checkbox' name=boxes[] value='Baseball' />Basketball
  27. <input type='checkbox' name=boxes[] value='Basketball' />Basketball
  28. <input type='checkbox' name=boxes[] value='Bowling' />Bowling
  29.  
  30. <input type='checkbox' name=boxes[] value='Rugby' />Rugby
  31. <input type='checkbox' name=boxes[] value='Tennis' />Tennis
  32. <input type='checkbox' name=boxes[] value='Valleyball' />Valleyball
  33.  
  34. <input type="button" id="btn" value="Zaznacz wszystkie" onclick="SetValues('sports', 'boxes[]');" />
  35.  
  36.  
  37. </form>
8_pasarzer_NOSTROMO
Git majonez,



"POMÓGŁ"
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.