【问题标题】:Why comparing checked attribute not working for checking at least one checkbox is checked?为什么比较选中的属性不适用于检查至少一个复选框被选中?
【发布时间】:2013-01-01 20:18:38
【问题描述】:

我做了这样的事情来检查所有复选框都被选中:

var container = $("#container");
$(container).find("input[type='checkbox'][checked != 'checked']").length == 0

但是当我使用选中属性来检查至少一个框被选中时,它总是给我长度 = 0,我的代码如下:

 $(container).find("input[type='checkbox'][checked = 'checked']").length > 0 

区别在于第一个代码 sn-p 我使用了 '!=' 而在第二个代码中我使用了“=”。那么为什么它在第二种情况下不起作用?

我知道我可以使用:

  $(container).find("input[type='checkbox']:is('checked')").length > 0 
  $(container).find("input[type='checkbox']:checked").length > 0

但仍然想了解为什么上面的东西不起作用:O

我不知道这是否是原因,但我选择了所有复选框,这是通过为所有复选框设置“已选中”属性值来实现的

$(container).find("input[type='checkbox']").attr("checked","checked")

对于取消选择我拥有的所有复选框:

$(container).find("input[type='checkbox']").removeAttr("checked","checked")

可能是因为我在取消选择时删除了 attr 检查,当我通过 UI 再次单击复选框时,我没有得到其选中的属性值。是这样吗?

【问题讨论】:

  • 检查你的容器是class还是id??
  • 它是一个 Id 并且在两种情况下都是相同的。
  • 对我来说很好,检查一下fiddle
  • 请注意,如果您已选中="true",该复选框仍处于选中状态,但您的条件不再返回 true
  • 请检查问题的编辑。

标签: jquery jquery-ui jquery-selectors


【解决方案1】:

那就试试这个

$('#container').find("input[type=checkbox]:checked").length > 0

$.each($("input[type=checkbox]:checked"), function() {

alert('Checked')

});

http://jsfiddle.net/kabichill/hAv3z/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多