复选框设置为选中=假不起作用 [英] checkbox set to checked = false not working

查看:153
本文介绍了复选框设置为选中=假不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用checked="false"生成HTML输入,但是该复选框显示为选中状态.

I'm generating an HTML input with checked="false", however the checkbox is showing up checked.

我在javascript控制台中进行了以下操作,无法完全了解发生了什么.使用.prop()将该值设置为false之后的结果HTML看起来相同,除了现在未在表单上选中此复选框.

I did the following in the javascript console and can't quite figure out whats going on. The resulting HTML after using .prop() to set the value to false looks the same except now the checkbox is not checked on the form.

> $(':input[checked]').prop('checked');
< true
> $(':input[checked]')
< [
<input type=​"checkbox" class=​"caseVal" checked=​"false">​
]
> $(':input[checked]').prop('checked',false);
< [
<input type=​"checkbox" class=​"caseVal" checked=​"false">​
]

我的印象是,我应该只设置checked="checked",或者,如果那是错误的最佳实践,那么根本不包括选中的属性?无论哪种方式,我都想知道上面的代码中发生了什么.

I'm under the impression that I should just be setting checked="checked" OR not including the checked property at all if its false is that best practice? Either way I'd like to know what's going on in the above code.

推荐答案

请勿放置checked="false"

您只将有效的XHTML设置为check ="checked",否则就可以

You only put checked="checked" for valid XHTML, otherwise you'd do

<input type="checkbox" checked>

浏览器不在乎将什么值分配给选中的属性,只要在复选框输入标签中看到checked,就会将其标记为选中.

The browser doesn't care what value is assigned to checked attribute, as soon as it sees checked in the checkbox input tag, it's flagged as checked.

这篇关于复选框设置为选中=假不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆