同时选中两个复选框 [英] Two Checkboxes Being Checked at the Same Time

查看:114
本文介绍了同时选中两个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个复选框.我需要的是,当有人选中其中一个框时,它也会自动选中另一个框.反之亦然,如果某人取消选中其中一个框,则取消选中这两个框.这是表格上的捆绑包,如果没有另一个,他们将无法获得一个.

I have two checkboxes. What I need is when someone checks one of the boxes, it will automatically check the other one as well. And vice versa, if someone unchecks one of the boxes, it unchecks both. This is a bundle package on the form and they can not get one without the other.

<input type="checkbox" id="chk1" name="chk1" value="100">Voicemail<br />
<input type="checkbox" id="chk2" name="chk2" value="50">VM Support

任何帮助将不胜感激!

推荐答案

假设您只想测试这两个复选框(而不是页面上的每个复选框),则可以使用

Assuming you want to only test these two checkboxes (and not every one on the page), you can use a jQuery Multiple Selector to access the onClick event for both. Using this you can test the checked status of the checkbox that was just clicked, and then assign that status of both checkboxes to match the one that was just clicked.

$('#chk1, #chk2').on('click', function(){
    var checked = $(this).is(':checked');
    $('#chk1, #chk2').attr('checked', checked);
});

这篇关于同时选中两个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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