Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [JavaScript] Sprawdzanie input radio z różnymi nazwami i nieznaną ilością input radio
harbii
post 2.04.2014, 22:25:51
Post #1





Grupa: Zarejestrowani
Postów: 311
Pomógł: 1
Dołączył: 27.05.2006

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


Kolejny problem z formularzem.

  1. var xxx = -1;
  2. var stan = document.getElementById('formularz').stan
  3.  
  4. for (x=0; x<stan.length; x++) {
  5. if (stan[x].checked) {
  6. xxx = x;
  7. break;
  8. }
  9. }
  10. if (xxx == -1) {
  11. alert('Nie zaznaczyles żadnego pola!');
  12. }


Powyższy skrypt sprawdza input radio z name=stan

A jak sprawdzić nieznaną ilość radio'nów, które tworz się same w sposób:
stan1
stan2
stan3
stan4 itd ?



Go to the top of the page
+Quote Post
Sephirus
post 3.04.2014, 07:42:56
Post #2





Grupa: Zarejestrowani
Postów: 1 527
Pomógł: 438
Dołączył: 28.06.2011
Skąd: Warszawa

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


http://jsfiddle.net/Q4U83/9/ Powinno pomóc wink.gif


--------------------
If you're good at something, never do it for free.
Potrzebujesz skryptu JS lub PHP - szukasz kogoś kto przetestuje twoją aplikację pod względem bezpieczeństwa? Szybko i solidnie? Napisz ;)
Mój blog - Jak zwiększyć wydajność front-endu - O buforowaniu wyjścia w PHP słów kilka...
Go to the top of the page
+Quote Post
harbii
post 3.04.2014, 13:21:52
Post #3





Grupa: Zarejestrowani
Postów: 311
Pomógł: 1
Dołączył: 27.05.2006

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


Fajne ale ten skrypcik co podrzuciłeś:
  1. <form id="formularz" method="post" onsubmit="return checkRadios();">
  2. <label><input type="radio" name="r1" value="0"/> aaa</label>
  3. <label><input type="radio" name="r1" value="1"/> bbb</label>
  4. <label><input type="radio" name="r1" value="2"/> ccc</label>
  5. <br/><br/>
  6. <label><input type="radio" name="r2" value="0"/> qqq</label>
  7. <label><input type="radio" name="r2" value="1"/> www</label>
  8. <label><input type="radio" name="r2" value="2"/> eee</label>
  9. <br/><br/>
  10. <label><input type="radio" name="r3" value="0"/> zzz</label>
  11. <label><input type="radio" name="r3" value="1"/> xxx</label>
  12. <label><input type="radio" name="r3" value="2"/> vvv</label>
  13. <br/><br/>
  14. <input type="submit" value="sprawdź!"/>
  15. </form>

  1. function checkRadios() {
  2. var inputs = document.getElementById('formularz').getElementsByTagName('input');
  3.  
  4. var noneChecked = true;
  5. for(var i = 0; i < inputs.length; i++) {
  6. if(inputs[i].type != 'radio') continue;
  7. if(inputs[i].checked) {
  8. noneChecked = false;
  9. break;
  10. }
  11. }
  12.  
  13. if(noneChecked) {
  14. alert('Nie zaznaczono żadnego radio!');
  15. } else {
  16. alert('Teraz formularz zostałby wysłany');
  17. }
  18.  
  19. return false; // wartosc zalezna od noneChecked
  20. }


Sprawdzi czy którykolwiek z inputów radio jest zaznaczony. Czyli wystarczuy zaznaczyć jeden i test przechodzi.
A mi chodzi o coś takiego ale ma sprawdzić czy input r1 był wybrany czy input2 był wybrany itd a nie którykolwiek.

Ten post edytował harbii 3.04.2014, 14:30:28
Go to the top of the page
+Quote Post
8_pasarzer_NOSTR...
post 8.04.2014, 08:15:22
Post #4





Grupa: Zarejestrowani
Postów: 109
Pomógł: 0
Dołączył: 5.11.2009

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


Witam

Podłącze się pod wątek.

Mam formularz, który sprawdza wszystkie radio w formularzu. A ja chce tylko wybrane.

  1. function checkRadio() {
  2. var inputs = document.getElementById('fo2').getElementsByTagName('input');
  3.  
  4. var noneChecked = true;
  5. for(var i = 0; i < inputs.length; i++) {
  6. if(inputs[i].type != 'radio') continue;
  7. if(inputs[i].checked) {
  8. noneChecked = false;
  9. break;
  10. }
  11. }
  12. if(noneChecked) {alert('Nie zaznaczono żadnego radio!');}
  13. return false;
  14. }


  1. <form method="post" id="fo2" onsubmit="return checkRadio();">
  2. $ile=count($id);
  3. for($x=0;x<=$ile;$x++)
  4. {<input type="radio" name="r1" value="0"/> aaa}
  5. <br />
  6. <input type="radio" name="r2" value="0"/> Opcja 1
  7. <input type="radio" name="r2" value="0"/> Opcja 2
  8. <input type="radio" name="r2" value="0"/> Opcja 3
  9. <input type="submit" value="Wyślij"/>


Chce żeby skrypt sprawdzał tylko pole name=r1. Bo tylko to jest wymagane, r2 może być puste.
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: 29.05.2025 - 08:11