jQuery检查隐藏的复选框onclick(jQuery check an hidden checkbox onclick)

编程入门 行业动态 更新时间:2024-10-05 19:12:15
jQuery检查隐藏的复选框onclick(jQuery check an hidden checkbox onclick)

我在jQuery 1.3.2上

我需要一个非常简单的代码。

我有4个复选框。

隐藏其中一个复选框。

我希望当单击3个可见复选框中的一个时,将自动检查隐藏的复选框。

所以我有

<input type="checkbox" style="display:none" id="hiddenOne"> <input type="checkbox" style="display:block" id="visible2"> <input type="checkbox" style="display:block" id="visible3"> <input type="checkbox" style="display:block" id="visible4">

因此,如果我点击任何visible2 3或4,将自动检查hiddenOne。

有没有办法在jQuery中做到这一点?

谢谢!

I am on jQuery 1.3.2

I need a very simple code.

I have 4 checkboxes.

One of these checkboxes is hidden.

I want that when one of the 3 visible checkboxes is clicked the hidden one will be automatically checked.

So I have

<input type="checkbox" style="display:none" id="hiddenOne"> <input type="checkbox" style="display:block" id="visible2"> <input type="checkbox" style="display:block" id="visible3"> <input type="checkbox" style="display:block" id="visible4">

So if I click on any visible2 3 or 4 the hiddenOne will be automatically checked.

Is there a way to do that in jQuery?

Thank you!

最满意答案

这将检查是否检查了三个可见的任何一个,如果有的则显示隐​​藏的那个并检查它。

http://jsfiddle.net/pxfunc/S6SFZ/

HTML:

<label id="hiddenLabel" style="display:none"><input type="checkbox" id="hiddenOne"> Hidden One</label> <label><input type="checkbox" id="visible2" class="visiChk"> Visible 2</label> <label><input type="checkbox" id="visible3" class="visiChk"> Visible 3</label> <label><input type="checkbox" id="visible4" class="visiChk"> Visible 4</label>

jQuery的:

// update if any of the three are checked/unchecked $('.visiChk').change(function() { var hiddenOne = $('#hiddenLabel')[0]; // Are any of them checked ? if ($('.visiChk:checked').length > 0) { hiddenOne.style.display = 'block'; hiddenOne.checked = true; } else { hiddenOne.style.display = 'none'; hiddenOne.checked = false; } });

this will check if any of the three visible's are checked, if any are then show the hidden one and check it.

http://jsfiddle.net/pxfunc/S6SFZ/

HTML:

<label id="hiddenLabel" style="display:none"><input type="checkbox" id="hiddenOne"> Hidden One</label> <label><input type="checkbox" id="visible2" class="visiChk"> Visible 2</label> <label><input type="checkbox" id="visible3" class="visiChk"> Visible 3</label> <label><input type="checkbox" id="visible4" class="visiChk"> Visible 4</label>

jQuery:

// update if any of the three are checked/unchecked $('.visiChk').change(function() { var hiddenOne = $('#hiddenLabel')[0]; // Are any of them checked ? if ($('.visiChk:checked').length > 0) { hiddenOne.style.display = 'block'; hiddenOne.checked = true; } else { hiddenOne.style.display = 'none'; hiddenOne.checked = false; } });

更多推荐

本文发布于:2023-08-07 11:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464076.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:复选框   onclick   jQuery   checkbox   hidden

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!