Check div is hidden using jquery

霸气de小男生 提交于 2019-12-03 06:20:43

问题


This is my div

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

Then I have a Show button that will show the div when you click:

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

So right now I want to check if the div #car2 is still hidden before form submission:

if($('#car2').is(':hidden')) {
    alert('car 2 is hidden');
}

Now here is the problem. Although the div #car2 already show, I still got alert message which means that jQuery assumes the div #car2 is still hidden.

My jQuery version is 1.7.

Thanks.

EDIT:

As jasper said, my code is correct and can be run via this demo.

What i suspect there is some conflict with jquery form to wizard plugin that i am using with my form. Anyone have any idea to solve this?


回答1:


You can check the CSS display property:

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

Here is a demo: http://jsfiddle.net/YjP4K/




回答2:


Try:

if(!$('#car2').is(':visible'))
{  
    alert('car 2 is hidden');       
}



回答3:


Try

if($('#car2').is(':hidden'))
{  
    alert('car 2 is hidden');       
}



回答4:


Try checking for the :visible property instead.

if($('#car2').not(':visible'))
{
    alert('car 2 is hidden');       
}



回答5:


Did you notice your typo, $car2 instead of #car2 ?

Anyway, :hidden seems to be working as expected, try it here.




回答6:


You can use,

if (!$("#car-2").is(':visible'))
{
      alert('car 2 is hidden');
}


来源:https://stackoverflow.com/questions/8484251/check-div-is-hidden-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!