有多个 checkbox 只能选择一个 当有两个都被勾选,最后选的会让之前选的被取消打钩 我用下面的方法
$(':checkbox[type="checkbox"]').each(function(){
$(this).click(function(){
if($(this).prop('checked')){
$(':checkbox[type="checkbox"]').removeAttr('checked');
$(this).prop('checked','true');
}
});
});
可是调试发现 前面的勾选依然显示打钩,虽然他们的属性 checked 为 false 了 怎么回事?
之前用的是
$(':checkbox[type="checkbox"]'). .prop('checked', false);
这个不仅显示没有取消打钩
连属性 checked 都继续为 true
疑问:
只能选择一个
为什么不用单选?
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>
</title>
</head>
<body>
<div id="jie">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
</div>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js">
</script>
<script>
//不需要什么each的
$('input[type="checkbox"]').on('change',function()
{
$('input[type="checkbox"]').prop('checked',false);
$(this).prop('checked',true);
});
</script>
</body>
</html>