jQuery 根据复选框值显示-隐藏 DIV
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
虽然这已经很旧了,但如果有人再次遇到这个问题(通过搜索)。从 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:
我使用 jQuery 属性
I use jQuery prop
这是因为您只选中当前复选框。
将其更改为
检查是否选中了任何复选框(此行中有很多检查..)。
参考: http://api.jquery.com/checked-selector/
That is because you are only checking the current checkbox.
Change it to
to check if any of the checkboxes is checked (lots of checks in this line..).
reference: http://api.jquery.com/checked-selector/
ebdiv 已设置
style="display:none;"
它可以显示 &隐藏
ebdiv is set
style="display:none;"
it is works show & hide
您可以考虑使用 jQuery 提供的
:checked
选择器。像这样的东西:You might consider using the
:checked
selector, provided by jQuery. Something like this:给所有使用 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 :'(
试试这个
,它会检查至少一个复选框是否被选中。
Try this
So it will check for at least one checkbox is checked or not.
`显示
$('#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
});`