Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [JavaScript] Sprawdzanie input radio z różnymi nazwami i nieznaną ilością input radio
Forum PHP.pl > Forum > Przedszkole
harbii
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 ?



Sephirus
http://jsfiddle.net/Q4U83/9/ Powinno pomóc wink.gif
harbii
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.
8_pasarzer_NOSTROMO
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.
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.