Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Dynamiczne zliczanie checkbox'ów
qbejs
post 25.07.2010, 22:30:09
Post #1





Grupa: Zarejestrowani
Postów: 48
Pomógł: 1
Dołączył: 9.02.2006

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


Witam,

Mam mały problem. Potrzebuje zliczyć dynamicznie ilość kliknietych checkbox'ów i nastepnie zapisac to w schowanym inpucie. Niestety jednak nie działa mi kod. Mnie wydaje się ok ale z JS miałem dotąd mało wspólnego.

  1. <script type=text/javascript>
  2. function Dodaj()
  3. {
  4. var ile_jest;
  5. if (document.getElementById('ile').value = '')
  6. {
  7. ile_jest = 0;
  8. }
  9. else
  10. {
  11. ile_jest = document.getElementById('ile').value;
  12. }
  13. var nowa += ile_jest;
  14. document.getElementById('ile').value = nowa;
  15. }
  16. </head>


oraz wywołanie

  1. <input type="checkbox" name="wybieram" id="wybieram" onSelect="Dodaj()" />


a ma to być składowane w

  1. <input type="hidden" name="ile" id="ile" ></input>


Jeżeli ma to coś wspólnego to kod jest tworzony niejako w locie przez PHP ( a dokladnie skrypt generujacy formularz ).

Pozdrawiam,
Kuba

Ten post edytował qbejs 25.07.2010, 22:40:31
Go to the top of the page
+Quote Post
thomson89
post 25.07.2010, 22:50:36
Post #2





Grupa: Zarejestrowani
Postów: 1 178
Pomógł: 51
Dołączył: 7.01.2009
Skąd: Gdańsk

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


1. W linijce 6 powinno być 2x '='
2. onselect nie działa na chekboxy

Takie coś działa doskonale:
  1. <script type=text/javascript>
  2. function dodaj()
  3. {
  4. var ile_jest;
  5. if (document.getElementById('ile').value == '')
  6. {
  7. ile_jest = 0;
  8. }
  9. else
  10. {
  11. ile_jest = document.getElementById('ile').value;
  12. ile_jest = parseInt(ile_jest);
  13. }
  14. ile_jest += 1;
  15. //alert(ile_jest);
  16. document.getElementById('ile').value = ile_jest;
  17. }
  18.  
  19. </head>
  20. <input type="checkbox" name="wybieram" id="wybieram" onChange="if(checked!=false)dodaj()" />
  21. <input type="checkbox" name="wybieram1" id="wybieram1" onChange="if(checked!=false)dodaj()" />
  22. <input type="checkbox" name="wybieram3" id="wybieram3" onChange="if(checked!=false)dodaj()" />
  23.  
  24.  
  25. <input type="text" name="ile" id="ile" value="0" />
  26. </body>
  27. </html>


--------------------
Sklep 70%
Go to the top of the page
+Quote Post
skowron-line
post 25.07.2010, 23:29:00
Post #3





Grupa: Zarejestrowani
Postów: 4 340
Pomógł: 542
Dołączył: 15.01.2006
Skąd: Olsztyn/Warszawa

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


@thomson89
  1. onChange="if(checked!=false)dodaj()"
a teraz pomnóż to przez 200 nie uważasz że lepiej to w funkcji sprawdzić zamiast
[JAVASCRIPT] pobierz, plaintext
  1. if (document.getElementById('ile').value == '')
[JAVASCRIPT] pobierz, plaintext
co wogóle jest bez sensu

Ten post edytował skowron-line 25.07.2010, 23:29:25


--------------------
I'm so fast that last night I turned off the light switch in my hotel room and was in bed before the room was dark - Muhammad Ali.
Peg jeżeli chcesz uprawiać sex to dzieci muszą wyjść, a jeżeli chcesz żeby był dobry ty też musisz wyjść - Al Bundy.

QueryBuilder, Mootools.net, bbcradio1::MistaJam
http://www.phpbench.com/
Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 19.07.2025 - 07:40