[英]How to uncheck checkboxes when check other one?
I have four checkboxes.Initially all are checked. 我有四个复选框,最初都被选中了。 When I click the none checkbox the other checkboxes are unchecked.
当我单击“无”复选框时,其他复选框均未选中。
html HTML
<label class='checkbox'><input type='checkbox' name='a' class="example" value='1000' checked/>A</label>
<label class='checkbox'><input type='checkbox' name='b' class="example" value='1000' class='termcls'checked />B</label>
<label class='checkbox'><input type='checkbox' name='c' class="example" value='1000' checked/>C</label>
<label class='checkbox'><input type='checkbox' name='none' onchange="uncheckOthers()" value='0' class='termcls'/>(None)</label>
jQuery jQuery的
function uncheckOthers()
{
$('input.example').not(this).prop('checked', false);
}
As mentioned by the Tushar , this
refers to the window
in uncheckOthers
not to the None
check-box element. 正如Tushar所提到的,
this
是指uncheckOthers
的window
, uncheckOthers
不是None
复选框元素。
If you are invoking inline function and expecting current element as this
in hadler, pass this
as argument. 如果您正在调用内联函数和期待当前元素为
this
在哈德勒,通过this
作为参数。
Try this: 尝试这个:
function uncheckOthers(elem) { $('input.example').not(elem).prop('checked', !elem.checked); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <label class='checkbox'> <input type='checkbox' name='a' class="example" value='1' checked/>A</label> <label class='checkbox'> <input type='checkbox' name='b' class="example" value='2' class='termcls' checked />B</label> <label class='checkbox'> <input type='checkbox' name='c' class="example" value='3' checked/>C</label> <label class='checkbox'> <input type='checkbox' name='none' onchange="uncheckOthers(this)" value='0' class='termcls' />(None)</label>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function () {
});
function uncheckOthers() {
$('input.example').prop('checked', false);
}
</script>
</head>
<body>
<label class='checkbox'><input type='checkbox' name='a' class="example" value='1' checked/>A</label>
<label class='checkbox'><input type='checkbox' name='b' class="example" value='2' class='termcls'checked />B</label>
<label class='checkbox'><input type='checkbox' name='c' class="example" value='3' checked/>C</label>
<label class='checkbox'><input type='checkbox' name='none' onchange="uncheckOthers()" value='0' class='termcls'/>(None)</label>
</body>
</html>
Just to complement the answer above I did a function to uncheck the none checkbox if one of anothers checkbox is checked. 只是为了补充上面的答案,我执行了一个功能,如果选中了另一个复选框,则取消选中无复选框。
function uncheckOthers(elem) { $('input.example').not(elem).prop('checked', !elem.checked); } function uncheckNone(elem) { if(elem.checked) { $('input.termcls').prop('checked', false); } }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <label class='checkbox'> <input type='checkbox' name='a' onchange="uncheckNone(this)" class="example" value='1' checked/>A</label> <label class='checkbox'> <input type='checkbox' name='b' onchange="uncheckNone(this)" class="example" value='2' class='termcls' checked />B</label> <label class='checkbox'> <input type='checkbox' name='c' onchange="uncheckNone(this)" class="example" value='3' checked/>C</label> <label class='checkbox'> <input type='checkbox' name='none' onchange="uncheckOthers(this)" value='0' class='termcls' />(None)</label>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.