使用 jQuery 检查 div 是否隐藏

发布于 2024-12-21 00:58:47 字数 800 浏览 2 评论 0原文

这是我的 div

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

然后我有一个显示按钮,当您点击时会显示 div:

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

所以现在我想检查 div #car2 在表单提交之前是否仍然隐藏:

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

现在这是问题。尽管 div #car2 已经显示,但我仍然收到警报消息,这意味着 jQuery 假定 div #car2 仍然隐藏。

我的 jQuery 版本是 1.7。

谢谢。

编辑:

正如贾斯珀所说,我的代码是正确的,可以通过这个演示< /a>.

我怀疑与 jQuery 表单向导有一些冲突我在表单中使用的插件。有人有办法解决这个问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

蒗幽 2024-12-28 00:58:47

您可以检查 CSS display 属性:

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

这是一个演示:http://jsfiddle.net/YjP4K/

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/

离笑几人歌 2024-12-28 00:58:47

尝试:

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

Try:

if(!$('#car2').is(':visible'))
{  
    alert('car 2 is hidden');       
}
淡水深流 2024-12-28 00:58:47

尝试

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

Try

if($('#car2').is(':hidden'))
{  
    alert('car 2 is hidden');       
}
深巷少女 2024-12-28 00:58:47

尝试检查 :visible 属性。

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

Try checking for the :visible property instead.

if($('#car2').not(':visible'))
{
    alert('car 2 is hidden');       
}
标点 2024-12-28 00:58:47

您是否注意到您的拼写错误,$car2 而不是 #car2

无论如何,:hidden 似乎按预期工作,请在此处尝试一下。

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

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

梦一生花开无言 2024-12-28 00:58:47

你可以使用,

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

You can use,

if (!$("#car-2").is(':visible'))
{
      alert('car 2 is hidden');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文