jQuery 根据复选框值显示-隐藏 DIV

发布于 2024-10-07 07:32:18 字数 534 浏览 0 评论 0原文

使用 AJAX,我用一堆复选框填充 DIV(每个复选框都有自己的唯一 ID)。 ID 为“projectID1”、“projectID2”、“projectID3”等等...我已为所有复选框指定了“pChk”类。

我的最终目标是在选中任何复选框时显示包含提交按钮的 DIV。包含“提交”按钮的 DIV 唯一显示隐藏的情况是所有复选框均未选中。

然而,我在下面提出的代码显示/隐藏每个复选框实例的提交按钮 DIV。换句话说,如果我选中了三个复选框,并且取消选中其中之一,则“提交按钮 DIV”将被隐藏。

非常欢迎您的专家建议!

function checkUncheck() { 
    $('.pChk').click(function() {
        if (this.checked) {
            $("#ProjectListButton").show();
        } else {
            $("#ProjectListButton").hide();
        }
    }); 
}

Using AJAX I populate a DIV with a bunch of checkboxes (each with it's own unique ID). The IDs are "projectID1", "projectID2", "projectID3" and so on... I have given all checkboxes a class of "pChk".

My end goal is to show the DIV containing the Submit Button if ANY of the checkboxes are checked. The only time the DIV containing the Submit Button show be hidden is if all checkboxes are unchecked.

However the code I have come up with below shows/hides the Submit Button DIV for each checkbox instance. In other words, if I have three checkboxes CHECKED and I UNCHECK one of them, the Submit Button DIV get hidden.

Your expert advice is more than welcome!

function checkUncheck() { 
    $('.pChk').click(function() {
        if (this.checked) {
            $("#ProjectListButton").show();
        } else {
            $("#ProjectListButton").hide();
        }
    }); 
}

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

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

发布评论

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

评论(8

命比纸薄 2024-10-14 07:32:18

虽然这已经很旧了,但如果有人再次遇到这个问题(通过搜索)。从 jQuery 1.7 开始,正确答案是:

$('.pChk').click(function() {
    if( $(this).is(':checked')) {
        $("#ProjectListButton").show();
    } else {
        $("#ProjectListButton").hide();
    }
}); 

While this is old if someone comes across this again (via search). The correct answer with jQuery 1.7 onwards is now:

$('.pChk').click(function() {
    if( $(this).is(':checked')) {
        $("#ProjectListButton").show();
    } else {
        $("#ProjectListButton").hide();
    }
}); 
初心未许 2024-10-14 07:32:18

我使用 jQuery 属性

$('#yourCheckbox').change(function(){
  if($(this).prop("checked")) {
    $('#showDiv').show();
  } else {
    $('#hideDiv').hide();
  }
});

I use jQuery prop

$('#yourCheckbox').change(function(){
  if($(this).prop("checked")) {
    $('#showDiv').show();
  } else {
    $('#hideDiv').hide();
  }
});
浴红衣 2024-10-14 07:32:18

这是因为您只选中当前复选框。

将其更改为

function checkUncheck() { 
    $('.pChk').click(function() {
        if ( $('.pChk:checked').length > 0) {
            $("#ProjectListButton").show();
        } else {
            $("#ProjectListButton").hide();
        }
    }); 
}

检查是否选中了任何复选框(此行中有很多检查..)。

参考: http://api.jquery.com/checked-selector/

That is because you are only checking the current checkbox.

Change it to

function checkUncheck() { 
    $('.pChk').click(function() {
        if ( $('.pChk:checked').length > 0) {
            $("#ProjectListButton").show();
        } else {
            $("#ProjectListButton").hide();
        }
    }); 
}

to check if any of the checkboxes is checked (lots of checks in this line..).

reference: http://api.jquery.com/checked-selector/

心是晴朗的。 2024-10-14 07:32:18

ebdiv 已设置 style="display:none;"

它可以显示 &隐藏

$(document).ready(function(){
    $("#eb").click(function(){
        $("#ebdiv").toggle();
    });

});

ebdiv is set style="display:none;"

it is works show & hide

$(document).ready(function(){
    $("#eb").click(function(){
        $("#ebdiv").toggle();
    });

});
请持续率性 2024-10-14 07:32:18

您可以考虑使用 jQuery 提供的 :checked 选择器。像这样的东西:

$('.pChk').click(function() {
    if( $('.pChk:checked').length > 0 ) {
        $("#ProjectListButton").show();
    } else {
        $("#ProjectListButton").hide();
    }
}); 

You might consider using the :checked selector, provided by jQuery. Something like this:

$('.pChk').click(function() {
    if( $('.pChk:checked').length > 0 ) {
        $("#ProjectListButton").show();
    } else {
        $("#ProjectListButton").hide();
    }
}); 
一枫情书 2024-10-14 07:32:18

给所有使用 Flat-Red、Flat-Green 插件的人一个提示,因为这个插件,上面的答案将不起作用!

在这种情况下,请使用 onchange="do_your_stuff();"在标签上,例如:
你的复选框在这里

它不起作用的原因是这个Jquery在真实的复选框周围创建了很多对象,所以你看不到它是否改变了。

但如果有人直接点击复选框,则不起作用:'(

A tip to all people that use flat-red, flat-green plugin, because of this plugin the answers above wont work!

In that case, use onchange="do_your_stuff();" on the label, for example:
Your checkbox here

The reason why it doesn't work is that this Jquery creates a lot of objects around the real checkbox, so you can't see if it's changed or not.

But if someone click straight on checkbox, won't work :'(

纸伞微斜 2024-10-14 07:32:18

试试这个

$('.yourchkboxes').change(function(){
    $('.yourbutton').toggle($('.yourchkboxes:checked').length > 0);
});

,它会检查至少一个复选框是否被选中。

Try this

$('.yourchkboxes').change(function(){
    $('.yourbutton').toggle($('.yourchkboxes:checked').length > 0);
});

So it will check for at least one checkbox is checked or not.

箜明 2024-10-14 07:32:18

`显示

$('#cbxShowHide').click(function(){
$('#block').show(1000):$('#block').hide(1000); this.checked?$('#block').show(1000):$('#block').hide(1000); //表演时间
});`

`Display

$('#cbxShowHide').click(function(){
this.checked?$('#block').show(1000):$('#block').hide(1000); //time for show
});`

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文