单选按钮检查属性没有设置为假
问题描述:
我有2个按钮yes
和no
,只是我正在切换它们。单选按钮检查属性没有设置为假
<!DOCTYPE html>
<html>
<head>
<title>Demo Project</title>
</head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src=""></script>
<script>
function enableCheckBoxAndDisableNoRadioButton() {
$(".checkbox").attr("checked",true);
$("#no").attr("checked", false);
}
function disableCheckBoxAnddisableYesRadioButton() {
$("#yes").attr("checked", false);
$(".checkbox").attr("checked",false);
}
</script>
<body>
<input class="checkbox" type="checkbox" >
<p>yes <input id="yes" type="radio" onchange="enableCheckBoxAndDisableNoRadioButton()"></p>
<p> no<input id="no" type="radio" checked=true onchange="disableCheckBoxAnddisableYesRadioButton()"></p>
</body>
</html>
当我点击yes
,那时没有按钮被取消选择,并且复选框被选中。 但是当我点击no
按钮时,那个时间复选框被取消选中,但是yes
按钮也被选中。我想让它取消选择。
让我知道是什么问题。
我在chrome上运行这段代码:53.0。
答
代替attr
,我将它更改为.prop
,它现在正在工作。
答
使用prop
insted attr
。
function enableCheckBoxAndDisableNoRadioButton() {
$(".checkbox").prop("checked", true);
$("#no").prop("checked", false);
}
function disableCheckBoxAnddisableYesRadioButton() {
$("#yes").prop("checked", false);
$(".checkbox").prop("checked", false);
}
答
变化.attr到.prop那么它的工作
答
你需要检查,我们可以使用.prop()
/.attr()
使用
.prop()
而不是.attr()
在大多数情况下。
请参见下面的属性列表中的一些财产:
| **Attribute/Property** | **Attribute** | Property
| accesskey | Yes | No
| align | Yes | No
| async | Yes | Yes
| autofocus | Yes | Yes
| checked | Yes | Yes
| class | Yes | No
| href | Yes | No
| multiple | Yes | Yes
| readonly | Yes | Yes
,并了解更多关于.prop() vs .attr()