Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [JavaScript] Sprawdzanie input radio z różnymi nazwami i nieznaną ilością input radio
harbii
post
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
 
Start new topic
Odpowiedzi
harbii
post
Post #2





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

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: 17.10.2025 - 08:55