Sprawdź div jest ukryty za pomocą jquery

This is my div

<div id="car2" style="display:none;"></div>

Następnie mam przycisk Pokaż, który pokaże div po kliknięciu:

$("show").click(function() {
    $("$car2").show();
}); 

Więc teraz chcę sprawdzić, czy div #car2 jest jeszcze ukryty przed wysłaniem formularza:

if($('#car2').is(':hidden')) {
    alert('car 2 is hidden');
}
Teraz jest problem. Chociaż div #car2 już się pokazuje, nadal mam komunikat alertu, co oznacza, że jQuery zakłada, że div #car2 jest nadal Ukryty.

Moja wersja jQuery to 1.7.

Dzięki.

EDIT:

Jak powiedział jasper, mój kod jest poprawny i można go uruchomić za pomocą tego demo .

Co podejrzewam jest jakiś konflikt z jQuery formularz do Kreatora wtyczki , że używam z moim formularzem. Czy ktoś ma jakiś pomysł, żeby to rozwiązać?

Author: cyberfly, 2011-12-13

6 answers

Możesz sprawdzić właściwość CSS display:

if ($('#car').css('display') == 'none') {
    alert('Car 2 is hidden');
}

Oto demo: http://jsfiddle.net/YjP4K/

 64
Author: Jasper,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-13 04:21:02

Try:

if(!$('#car2').is(':visible'))
{  
    alert('car 2 is hidden');       
}
 30
Author: Omtara,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-13 04:22:27

Try

if($('#car2').is(':hidden'))
{  
    alert('car 2 is hidden');       
}
 9
Author: Sadikhasan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-02-20 08:01:29

Spróbuj sprawdzić właściwość: visible.

if($('#car2').not(':visible'))
{
    alert('car 2 is hidden');       
}
 4
Author: Phil.Wheeler,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-13 04:22:06

Zauważyłeś literówkę, $car2 zamiast #car2 ?

W każdym razie, :hidden wydaje się działać zgodnie z oczekiwaniami, spróbuj tutaj .

 2
Author: smendola,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-13 10:43:54

Możesz użyć,

if (!$("#car-2").is(':visible'))
{
      alert('car 2 is hidden');
}
 1
Author: Shar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-05 08:40:01