jQuery基于复选框值显示隐藏DIV [英] jQuery Show-Hide DIV based on Checkbox Value

查看:160
本文介绍了jQuery基于复选框值显示隐藏DIV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用AJAX我填充一个DIV一个复选框(每个都有自己的唯一ID)。 ID是projectID1,projectID2,projectID3等等...我已经给所有的复选框一类pChk。

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".

我的最终目标如果选中任何复选框,则显示包含提交按钮的DIV。

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.

但是我在下面显示的代码显示/隐藏了提交按钮DIV对于每个复选框实例。换句话说,如果我有三个复选框CHECKED和我UNCHECK其中一个,提交按钮DIV隐藏。

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();
        }
    }); 
}


推荐答案

再次遇到这个(通过搜索)。 jQuery 1.7的正确答案现在是:

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();
    }
}); 

这篇关于jQuery基于复选框值显示隐藏DIV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆