问题描述
我有三个复选框在一行,这个代码我发现工作伟大
I have 3 checkboxes in a row and this code I found works GREAT
$('input:checkbox').click(function () {
$(this).closest('tr').find('input:checkbox').prop('checked', false);
$(this).prop('checked', true);
});
但是,我有一个业务需求,我被告知如果有人点击已经检查,它必须取消选中。是的,我知道单选按钮通常用于只允许1项目被选中,然而即使单选按钮也不是可以点击取消选择。
However, I have a business requirement in which I'm told if someone clicks on a checkbox that is already checked, that it must uncheck. Yes I understand that radio buttons typically are used in the "only allow 1 item to be selected" however even a radio button is not something you can click to "de-select"
复选框是更自然的,能够取消选择,但使用我的功能,我正在尝试取消选择,如果已经选择并点击,它总是转到else语句
Checkboxes are more natural to be able to un-select but with my function I was trying unselect if already selected and clicked and it always goes to the else statement
$('input:checkbox').click(function () {
//alert('c');
$(this).closest('tr').find('input:checkbox').prop('checked', false);
if ($(this).checked)
{
alert('f');
$(this).prop('checked', false);
}
else {
alert('t');
$(this).prop('checked', true);
}
});
摘要
有3个复选框,目前我的功能只允许检查其中1个。但现在我需要点击当前选中的复选框并取消选中。
There are 3 checkboxes , currently my function only allows 1 of them to be checked. However now I need to be able to click on the currently checked checkbox and uncheck it
推荐答案
在该行的复选框上设置 checked = false
时,忽略点击的复选框它将像一个普通的复选框,例如:
Just omit the clicked checkbox when setting checked = false
on checkboxes in that row, so that it will behave like a regular checkbox, eg:
$(this).closest('tr').find('input:checkbox').not(this).prop('checked', false);
这篇关于如果用jquery选中,取消选中复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!