Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [jquery] jak zrobic LUB
nieraczek
post
Post #1





Grupa: Zarejestrowani
Postów: 405
Pomógł: 6
Dołączył: 12.01.2007

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


Mam przykładowo:
  1. $(document).ready(function()
  2. {
  3. $('#sf_guard_user_username').blur(function()
  4. {
  5. if($('#sf_guard_user_username').val().length == 0)
  6. {
  7. $('#sf_guard_user_username').css('background-color', 'orange');
  8. $('#rejestracja1').html('Login musi zawierać minimum 5. znaków.');
  9. }
  10. }
  11. });


Czyli, że po wyjściu z inputa jak nic w nim nie wpisano to wyświetla błąd, a jak zrobić żeby było: jeśli nastąpiło wyjście z inputa '#sf_guard_user_username' LUB naciśnięto button '#register':

bo tak nie działa:
  1. if($('#sf_guard_user_username').blur || $('#register').click)(function()


Czy zostaje tylko skopiowanie powyższego kodu i zamiana $('#sf_guard_user_username').blur(function() na $('#register').click(function() questionmark.gif
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 6)
Hazel
post
Post #2





Grupa: Zarejestrowani
Postów: 492
Pomógł: 33
Dołączył: 16.08.2007
Skąd: Wrocław

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


Nie, możesz jeszcze olać funkcje anonimowe i użyć normalnej, nazwanej funkcji:

Kod
$('#sf_guard_user_username').blur = twojaFunkcja;

$('#register').click = twojaFunkcja;

function twojaFunkcja()

{

if($('#sf_guard_user_username').val().length == 0) 
{
$('#sf_guard_user_username').css('background-color', 'orange');
$('#rejestracja1').html('Login musi zawierać minimum 5. znaków.');
}



}




Funkcje anonimowe nie zawsze są najlepszym rozwiązaniem.


--------------------
Go to the top of the page
+Quote Post
nieraczek
post
Post #3





Grupa: Zarejestrowani
Postów: 405
Pomógł: 6
Dołączył: 12.01.2007

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


Zrobiłem tak:

  1. <script type="text/javascript">
  2. $(document).ready(function()
  3. {
  4. $('#sf_guard_user_username').blur = twojaFunkcja();
  5.  
  6. $('#register').click = twojaFunkcja();
  7.  
  8.  
  9. function twojaFunkcja()
  10. {
  11. if($('#sf_guard_user_username').val().length == 0)
  12. {
  13. $('#sf_guard_user_username').css('background-color', 'orange');
  14. $('#rejestracja1').html('Login musi zawierać minimum 5. znaków.');
  15. }
  16. else
  17. {
  18. $('#sf_guard_user_username').css('background-color', null);
  19. $('#rejestracja1').html('');
  20. }
  21. }
  22.  
  23. });



Ale już na wstępie jak wejdę na stronę to mam to pole podświetlone jako błedne, nawet jak zakomentowalem: $('#sf_guard_user_username').blur = twojaFunkcja(); (dla pewności że jakimś cuden kursor sam nie wchodzi i wychodzi z tego pola tongue.gif ) hm......... ?

Jak tą funkcję przeniosę nad $(document).ready(function() to i tak jest tak samo.

Ten post edytował nieraczek 12.03.2009, 17:54:08
Go to the top of the page
+Quote Post
erix
post
Post #4





Grupa: Moderatorzy
Postów: 15 467
Pomógł: 1451
Dołączył: 25.04.2005
Skąd: Szczebrzeszyn/Rzeszów




Kod
$('#sf_guard_user_username').blur = twojaFunkcja();

A nie powinno być czasem:
Kod
$('#sf_guard_user_username').blur = twojaFunkcja;

?


--------------------

ZCE :: Pisząc PW załączaj LINK DO TEMATU i TYLKO w sprawach moderacji :: jakiś błąd - a TREŚĆ BŁĘDU? :: nie ponaglaj z odpowiedzią via PW!
Go to the top of the page
+Quote Post
nieraczek
post
Post #5





Grupa: Zarejestrowani
Postów: 405
Pomógł: 6
Dołączył: 12.01.2007

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


Zapomniałem napisać, więc teraz to zrobię tongue.gif:
  1. $('#sf_guard_user_username').blur = twojaFunkcja;


W ogóle nie działa, dlatego próbowałem z ()
Go to the top of the page
+Quote Post
Hazel
post
Post #6





Grupa: Zarejestrowani
Postów: 492
Pomógł: 33
Dołączył: 16.08.2007
Skąd: Wrocław

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


Niemożliwe, testowałem u siebie i działa. To znaczy nie testowałem jQuery, a zdarzenia onblur i onclick, ale to wychodzi na to samo. Chyba że jQuery ma jakieś błędy.


--------------------
Go to the top of the page
+Quote Post
nieraczek
post
Post #7





Grupa: Zarejestrowani
Postów: 405
Pomógł: 6
Dołączył: 12.01.2007

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


Mam rozwiązanie - troszeczkę dziwne - anonimowa funkcja wywoluje nazwana funkcje:

  1. $('#sf_guard_user_username').blur(function()
  2. {
  3. twojaFunkcja();
  4. });
  5.  
  6. $('#register').click(function()
  7. {
  8. twojaFunkcja();
  9. });


smile.gif
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 Aktualny czas: 19.08.2025 - 19:50