简体   繁体   English

HTML单选和复选框未在内部检查/取消选中

[英]HTML radio and checkboxes not checking/unchecking internally

So visually they have the correct behavior. 因此,从视觉上看,它们具有正确的行为。 Only 1 radio button in a set is checked and the checkbox checks/unchecks in reaction to pressing it but it seems when checking the status of the inputs in the console that isn't the case. 一组中仅选中了一个单选按钮,并且复选框在按下时会进行检查/取消选中,但是在检查控制台中的输入状态时似乎不是这样。 So I have: 所以我有:

<input id="addon-fixed" type="checkbox" value=True checked />

<input id="addon-type0" name="addon-type" type="radio" checked/>Addon<br>
<input id="addon-type1" name="addon-type" type="radio"/>Cutout

But regardless of what I click the behavior is always the same 但是无论我单击什么,行为总是相同的

$('#addon-fixed').attr('checked')   // always there

$('#addon-type0').attr('checked')   // always there 
$('#addon-type1').attr('checked')   // always undefined

Use prop() instead of attr() 使用prop()而不是attr()

$('#addon-fixed').prop('checked')

Checking or unchecking the checkbox changes the checked property , it doesn't change the element's attribute . 选中或取消选中此复选框将更改checked 属性 ,但不会更改元素的attribute

if you want to check whether it is checked or not: Use this... 如果要检查是否已选中:请使用此...

if($('#addon-fixed').is(':checked')){
//checked
}else {
//unchekced
}

hope this will help you... 希望这个能对您有所帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM