Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [js] walidacja radio
agataperz
post
Post #1





Grupa: Zarejestrowani
Postów: 182
Pomógł: 0
Dołączył: 24.03.2009
Skąd: Hamburg DE

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


Witam,

mam taki skrypt
  1. function checkradio(){
  2. var radio=false;
  3. for (var i=0; i<document.formularz.length; i++){
  4. if (document.formularz.elements[i].type == "radio"){
  5. var x = document.formularz.elements[document.formularz.elements[i].name].length;
  6. for (var y=0; y<x; y++){
  7. if (document.formularz.elements[i+y].checked) break;
  8. }
  9. if (y == x){
  10. return false;
  11. } else radio=true;
  12. i = i+x-1;
  13. } else radio=true;
  14. }
  15. return radio;
  16. }


oraz

  1. <form name="formularz" action="finalizacja.php" method="post" onsubmit="return sprawdz(this);return checkradio(this)">


funkcja sprawdz działa poprawie - sprawdza pola input text
niestety funkcja checkradio nie chce działać. możecie mi powiedzieć co jest źle?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
CuteOne
post
Post #2





Grupa: Zarejestrowani
Postów: 2 958
Pomógł: 574
Dołączył: 23.09.2008
Skąd: wiesz, że tu jestem?

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


W ramach testów stwórz sobie nowy plik index.html a w nim:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2" />
  4. <title>Untitled Document</title>
  5. </head>
  6.  
  7.  
  8. <form name="formularz" id="formularz" action="ddd.thml" onsubmit="validate(); return false;">
  9. <input type="radio" name="platnosc" value="1"/>
  10. <input type="radio" name="platnosc" value="2"/>
  11. <input type="radio" name="platnosc" value="3"/>
  12.  
  13. <input type="submit" name="submit"/>
  14. </form>
  15.  
  16. <script type="text/javascript">
  17.  
  18. function checkRadio(inputName) {
  19.  
  20. var obj = document.formularz[inputName];
  21. var len = obj.length;
  22.  
  23. for(var i=0; i<len; i++) {
  24.  
  25. if(obj[i].checked) return true;
  26.  
  27. }
  28. alert('Radio nie został wciśnięty');
  29. return false;
  30. }
  31.  
  32.  
  33. function validate(form) {
  34.  
  35. var noerror = true;
  36.  
  37. if(!checkRadio('platnosc')) { noerror = false; }
  38. // itd. np. sprawdzanie inputów if(checkInput()) { noerror = false; }
  39.  
  40. if(noerror) {
  41.  
  42. document.formularz.submit();
  43. }
  44. }
  45. </script>
  46.  
  47. </body>
  48. </html>


Ten post edytował CuteOne 28.08.2011, 14:23:04
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: 9.10.2025 - 01:47