Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> czy element ma klase z jquery, co robie nie tak ?
asall
post
Post #1





Grupa: Zarejestrowani
Postów: 42
Pomógł: 0
Dołączył: 11.12.2013

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


co robie nie tak ?

<script>
if ($("input[name=zdjecie_1]").hasClass("error_pole")) {$( "#myDiv" ).show();}else{$( "#myDiv" ).hide();}
</script>
<div id="myDiv"><label class="error_pole">Pole wymagane</label></div>
Go to the top of the page
+Quote Post
trueblue
post
Post #2





Grupa: Zarejestrowani
Postów: 6 806
Pomógł: 1828
Dołączył: 11.03.2014

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


Zależy co chcesz zrobić.
W przykładzie sprawdzasz czy <input name="zdjecie_1"/> ma klasę "error_pole", a poniżej masz label z tą klasą. Albo pokazałeś za mało kodu, albo sprawdzasz inny element niż chcesz, albo sprawdzasz dobry, a w kodzie masz label zamiast input.
Go to the top of the page
+Quote Post
asall
post
Post #3





Grupa: Zarejestrowani
Postów: 42
Pomógł: 0
Dołączył: 11.12.2013

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


jeżeli input zdjęcie_1 ma klase error_pole wyświetlić DIV myDiv jeżeli nie to ukryć
Go to the top of the page
+Quote Post
trueblue
post
Post #4





Grupa: Zarejestrowani
Postów: 6 806
Pomógł: 1828
Dołączył: 11.03.2014

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


Używasz jQuery? Załączyłeś w kodzie tą bibliotekę?

Jeśli jQuery, to:
  1. <script type="text/javascript">
  2. $(document).ready(function(){
  3.  
  4. tu twój kod
  5.  
  6. });
Go to the top of the page
+Quote Post
sunpietro
post
Post #5





Grupa: Zarejestrowani
Postów: 262
Pomógł: 26
Dołączył: 23.01.2009
Skąd: eZ Systems

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


popraw selektor:
Kod
if ($('input[name="zdjecie_1"]') {}

Go to the top of the page
+Quote Post
trueblue
post
Post #6





Grupa: Zarejestrowani
Postów: 6 806
Pomógł: 1828
Dołączył: 11.03.2014

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


@Kolega ma dobry wzrok.
A przy okazji jest jeszcze inne rozwiązanie:
  1. if ($('input[name="zdjecie_1"].error_pole'))
Go to the top of the page
+Quote Post
nospor
post
Post #7





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




@trueblue Twoje ostatnie rozwiazanie jest bledne gdyz jQuery zawsze zwraca obiekt, niezaleznie czy cos znalazl czy nie, wiec IF zawsze bedzie wykonany
Go to the top of the page
+Quote Post
trueblue
post
Post #8





Grupa: Zarejestrowani
Postów: 6 806
Pomógł: 1828
Dołączył: 11.03.2014

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


Racja, cóż, przyzwyczajenie z MooTools.
Wyrzucić, albo zostawić dla potomnych:)

Ale modyfikując:
  1. if($('input[name="zdjecie_1"].error_pole').length>0)

Choć z hasClass jest czytelniejsze.
Go to the top of the page
+Quote Post

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: 24.08.2025 - 04:18