javascript - jquery attr ('checked',错误)错误

标签 javascript jquery

使用 jquery 1.8 版,我可以通过 $el.attr('checked',logic_value) 选中和取消选中复选框:

http://jsbin.com/oyIQuJo/1/edit

使用jquery 版本1.10.1(或2.0.2),它仅更改一次为选中,然后变为未选中,并且我无法将其设置为再次选中:

http://jsbin.com/oyIQuJo/2/edit

http://jsbin.com/oyIQuJo/3/edit

这是一个已知的行为/错误吗?最好的方法是什么,以便我可以多次切换它的选中/未选中状态?

我可以做 $el[0].checked =logic_value ( http://jsbin.com/oyIQuJo/4/edit ),但我不太喜欢这个解决方案,因为那样我应该循环选择器的所有元素如果它有多个元素。

最佳答案

您必须使用.prop()而不是.attr()。因为 .prop() 实际上是为了设置 checkedselected 等属性而设计的。

正确的方法是,

$el.prop('checked', logic_value)

请阅读here掌握 .prop()

关于javascript - jquery attr ('checked',错误)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20501415/

相关文章:

javascript - 我应该使用什么作为 Electron 应用程序的后端?

javascript - 使用变量的语句声明?

javascript - Chart JS 图例样式

javascript - 自动点击页面上的div

javascript - 将可调整大小的 jQuery UI 锁定为仅垂直调整大小?

javascript - Symfony2/Javascript 删除警报

JavaScript 或 jQuery 字符串以实用函数结尾

javascript - 在 jQuery 中单击 fadeToggle

javascript - 从选定的 OrgChart 节点获取值 (Google orgchart)

javascript - 如何使用 jQuery 比较 tr 中的几个 td.text()