Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [JavaScript]Wykonywanie funkcji bez reloadu
matjanos
post 9.07.2009, 14:38:28
Post #1





Grupa: Zarejestrowani
Postów: 30
Pomógł: 0
Dołączył: 3.03.2008
Skąd: Toruń

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


Witam.
Mam krótki formularz i chciałbym, aby po każdym przełączeniu focusa (kliknięciu kursorem w inny input) była wykonywana funkcja sprawdzająca poprawność zawartości własnie opuszczonego inputa.
Funkcja ta wykorzystująca innerHTML wyświetlałaby odpowiedni tekst obok inputa.
Chodzi o coś w rodzaju techniki wykorzystanej w formularzu rejestracyjnym forum.php.pl
Go to the top of the page
+Quote Post
skowron-line
post 9.07.2009, 14:47:11
Post #2





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

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


  1. <script type="text/javascript">
  2. function sprawdz( obj )
  3. {
  4. if( obj.value == "" )
  5. {
  6. alert( 'blad' );
  7. }
  8. }
  9.  
  10.  
  11. <input type="text" onclick="sprawdz( this );" onblur="sprawdz( this );">


*pisane z palca, reszte doczytaj.


--------------------
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
piotrooo89
post 9.07.2009, 14:50:24
Post #3


Newsman


Grupa: Moderatorzy
Postów: 4 005
Pomógł: 548
Dołączył: 7.04.2008
Skąd: Trzebinia/Kraków




z palucha:

  1. function pokaz(co, gdzie)
  2. {
  3. var gdzie = document.getElementById(gdzie);
  4. gdzie.innerHTML=co;
  5. }
  6. <form action="" method="get">
  7. <input type="text" name="t1" id="t1" onBlur="pokaz(this.value, 't1l')" /><span id="t1l"></span>
  8. <input type="text" name="t2" id="t2" onBlur="pokaz(this.value, 't2l')" /><span id="t2l"></span>
  9. </form>


--------------------
Go to the top of the page
+Quote Post
matjanos
post 10.07.2009, 15:50:30
Post #4





Grupa: Zarejestrowani
Postów: 30
Pomógł: 0
Dołączył: 3.03.2008
Skąd: Toruń

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


Bardzo mi pomogło! Dzięki!
Nie chcą zakładać nowego tematu zapytam od razu w tym o jeszcze jedną rzecz.
Mam taki kod (wyślę cały bo nie wiem co tu sie może przydać):

Kod
<script>
function pokaz(co, gdzie)
{
var gdzie = document.getElementById(gdzie);

if(co==""){gdzie.innerHTML="<font color='red'><ul style='list-style-image:url(images/no.gif);'><li>Błąd: Wpisz komentarz.</li></ul></font>";
gdzie1.innerHTML="<input type='submit' value='Wyślij' style='margin-left:63px; float:left;' name='sub' disabled='disabled'>";}
else if(co=="Treść komentarza"){gdzie.innerHTML="<font color='red'><ul style='list-style-image:url(images/no.gif);'><li>Błąd: Zmień komentarz z domyślnego</li></ul></font>";
gdzie1.innerHTML="<input type='submit' value='Wyślij' style='margin-left:63px; float:left;' name='sub' disabled='disabled'>";}
else gdzie.innerHTML="<font color='green'><ul style='list-style-image:url(images/yes.gif);'><li>Poprawne dane</li></ul></font>";}

function pokaz1(co, gdzie)
{
var gdzie = document.getElementById(gdzie);


if(co==""){gdzie.innerHTML="<font color='red'><ul style='list-style-image:url(images/no.gif);'><li>Błąd: Wpisz nick</li></ul></font>";
gdziesub.innerHTML="<input type='submit' value='Wyślij' style='margin-left:63px; float:left;' name='sub' disabled='disabled'>";}
else gdzie.innerHTML="<font color='green'><ul style='list-style-image:url(images/yes.gif);'><li>Poprawne dane</li></ul></font>";

}

</script>



<form action='wpisy.php' method='post'>
<div style='margin-left:60px;'>Twój nick:</div>
<span id='nick1' style="font-size:13px; float:right; line-height:1em; margin-right:70px;"> </span>
<input type="text" name="nick" onBlur="pokaz1(this.value, 'nick1')" style="float:left; margin-right:25px;margin-left:60px;"><div style="text-align:left; float:left;">
<span id='nick1' style="font-size:13px; float:right;"> </span>
<input type="checkbox" name="an" onclick="if(this.checked==true){ nick.disabled=true; nick.value='Anonim';}else{ nick.disabled=false; nick.value='';}"
id="an" style='float:both; margin-left:60px;' /><label for="an" style="font-size:10px; color:grey;">Zaznacz jeśli chcesz być anonimowy</label></div><br />




<textarea name="com" rows="8" cols="35" style="color: gray; font-style: italic; margin-left:60px; " onBlur="pokaz(this.value, 'com1')">Treść komentarza</textarea><br />



<input type="submit" value="Wyślij" style="margin-left:60px; float:left;" name="sub" id="sub" OnMouseOver="pokaz(blad, 'val')">&nbsp;&nbsp;&nbsp;&nbsp;
<span style="font-size:13px; margin-bottom:15px;" id='com1'> </span> <br />
</form>

I chciałbym żeby podczas kiedy ktoś błędnie uzupełni pole blokował się guzik submit.
co zrobic?
Go to the top of the page
+Quote Post
skowron-line
post 10.07.2009, 15:52:21
Post #5





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

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


  1. document.getElementsByName( 'sub' )[0].disabled = true;

To zablokuje submita.


@UP
piotrooo89 takim sposobem robi się lekki bajzel w kodzie lepiej robić to dynamicznie
javascript nextsibling
  1. <script type="text/javascript" src="advajax.js"></script>
  2. <script type="text/javascript">
  3.  
  4. function insertAfter( referenceNode, newNode )
  5. {
  6. referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
  7. }
  8.  
  9. function request( object, file )
  10. {
  11. var oD = document.createElement( 'div' );
  12. insertAfter( object, oD );
  13.  
  14. advAJAX.get({
  15. url: file,
  16. mimeType: 'text/html',
  17. onSuccess : function(obj)
  18. {
  19. oD.innerHTML = obj.responseText;
  20. }
  21. });
  22. object.setAttribute( 'onclick',"" );// special 4 IE
  23.  
  24. }
  25.  
  26. </head>
  27. Just one click <br />
  28. <a href="javascript:void(0);" onclick="javascript:request( this, 'a.txt' );">Click me 2 get text from A file</a><br />
  29. <a href="javascript:void(0);" onclick="javascript:request( this, 'b.txt' );">Click me 2 get text from B file</a><br />
  30. <a href="javascript:void(0);" onclick="javascript:request( this, 'a.txt' );">Click me 2 get text from A file</a><br />
  31. <a href="javascript:void(0);" onclick="javascript:request( this, 'b.txt' );">Click me 2 get text from B file</a><br />
  32. <a href="javascript:void(0);" onclick="javascript:request( this, 'a.txt' );">Click me 2 get text from A file</a><br />
  33. <a href="javascript:void(0);" onclick="javascript:request( this, 'b.txt' );">Click me 2 get text from B file</a><br />
  34. <a href="javascript:void(0);" onclick="javascript:request( this, 'a.txt' );">Click me 2 get text from A file</a><br />
  35. <a href="javascript:void(0);" onclick="javascript:request( this, 'b.txt' );">Click me 2 get text from B file</a><br />
  36. <a href="javascript:void(0);" onclick="javascript:request( this, 'a.txt' );">Click me 2 get text from A file</a><br />
  37. <a href="javascript:void(0);" onclick="javascript:request( this, 'b.txt' );">Click me 2 get text from B file</a><br />
  38. <a href="javascript:void(0);" onclick="javascript:request( this, 'a.txt' );">Click me 2 get text from A file</a><br />
  39. <a href="javascript:void(0);" onclick="javascript:request( this, 'b.txt' );">Click me 2 get text from B file</a><br />
  40. <a href="javascript:void(0);" onclick="javascript:request( this, 'a.txt' );">Click me 2 get text from A file</a><br />
  41. <a href="javascript:void(0);" onclick="javascript:request( this, 'b.txt' );">Click me 2 get text from B file</a><br />
  42. <a href="javascript:void(0);" onclick="javascript:request( this, 'a.txt' );">Click me 2 get text from A file</a><br />
  43. <a href="javascript:void(0);" onclick="javascript:request( this, 'b.txt' );">Click me 2 get text from B file</a><br />
  44. <a href="javascript:void(0);" onclick="javascript:request( this, 'a.txt' );">Click me 2 get text from A file</a><br />
  45. <a href="javascript:void(0);" onclick="javascript:request( this, 'b.txt' );">Click me 2 get text from B file</a><br />
  46. <a href="javascript:void(0);" onclick="javascript:request( this, 'a.txt' );">Click me 2 get text from A file</a><br />
  47. <a href="javascript:void(0);" onclick="javascript:request( this, 'b.txt' );">Click me 2 get text from B file</a><br />
  48. <a href="javascript:void(0);" onclick="javascript:request( this, 'a.txt' );">Click me 2 get text from A file</a><br />
  49. <a href="javascript:void(0);" onclick="javascript:request( this, 'b.txt' );">Click me 2 get text from B file</a><br />
  50. <a href="javascript:void(0);" onclick="javascript:request( this, 'a.txt' );">Click me 2 get text from A file</a><br />
  51. <a href="javascript:void(0);" onclick="javascript:request( this, 'b.txt' );">Click me 2 get text from B file</a><br />
  52. </body>
  53. </html>

Przykład wklejam bo nie mogłem znaleść adresu na który to wrzuciłem.

Ten post edytował skowron-line 10.07.2009, 15:55:34


--------------------
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: 20.07.2025 - 06:12