[英]Toggle between 2 check boxes using jquery or javascript
I'm having difficulty in using jquery to toggle between 2 checkboxes..Currently this is what i have. 我在使用jquery在2个复选框之间切换时遇到了困难。目前这就是我所拥有的。
<div id="chkBoxList" onclick="CheckBoxToggle()">
<font size="2">
<input type="checkbox" class="all" name="empType" id="empType1" value="BWR is a Full Time employee. " checked="checked" disabled="disabled"/>
Full Time
<input type="checkbox" class="all" name="empType" id="empType2" value="BWR is a Part Time employee. " /> Part Time
</font>
</div>
jquery follows thus. 因此jquery就这样了。
function CheckBoxToggle() {
$('#chkBoxList').click(function () {
var $checkbox = $(this).find(':checkbox');
$checkbox.prop('checked', !$checkbox[0].checked);
$checkbox[1].checked;
$checkbox.trigger('change');
});
}
But this either toggles both to checked or unchecked. 但这要么切换到选中或未选中。 Since, initial value for one is checked and disabled, when clicked on second i want other to be checked and disabled while the first will be unchecked and disabled. 因为,检查和禁用了一个的初始值,当点击第二个时我想要检查和禁用其他值,而第一个将被取消选中并禁用。 Any help is appreciated. 任何帮助表示赞赏。
<html>
<body>
<form name=f1 id=f1>
<input type=checkbox name=chk1 id=chk1 onclick=javascript:document.f1.chk2.checked=!document.f1.chk1.checked;> Check 1<BR>
<input type=checkbox name=chk2 id=chk2 onclick=javascript:document.f1.chk1.checked=!document.f1.chk2.checked;> Check 2<BR>
</form>
</body>
</html>
$('input.all').click(function(e){
$('input.all').prop('checked', false).prop('disabled',false) ;
$(this).prop('checked', true).prop('disabled', true) ;
}) ;
You need to use .each() to loop through $checkbox. 您需要使用.each()循环$ checkbox。
$(function(){
$('#chkBoxList').click(function () {
var $checkbox = $(this).find(':checkbox');
$checkbox.each(function(){
$(this).prop('checked', !$checkbox[0].checked);
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.