【问题标题】:jQuery and IE: Checking for hidden inputs "checked"-attribute does not workjQuery 和 IE:检查隐藏输入“已检查”属性不起作用
【发布时间】:2013-09-07 17:29:48
【问题描述】:
我一直在尝试检查我的 input[type=hidden] 是否具有 checked="checked" 属性。
该输入字段如下所示:
<input type="hidden" checked="checked" name="1_WILLINFOS">
在 Firefox 中,使用以下命令可以正常工作:
jQuery(selector).find('input[name=1_WILLINFOS]').attr('checked') == "checked"
但是IE10总是给我一个假的。
到目前为止,我尝试使用以下内容:
.attr("checked") == "checked"
.attr("checked") == true
.is(':checked')
他们都给我一个假的。
有什么想法吗?
谢谢
【问题讨论】:
标签:
javascript
jquery
internet-explorer
hidden
checked
【解决方案1】:
您不能将隐藏输入设为复选框,因此它不会有 checked 属性。您可以使用type="checkbox" 并使用display:none 隐藏它,如果您需要它是一个复选框,或者您可以简单地使用带有0 或1 的隐藏输入来模拟它。
【解决方案2】:
你可以拥有任何属性的隐藏元素。
例如
<input type="hidden" abc="xyz" name="1_WILLINFOS">//is an `input` type `hidden` with attribute abc having value xyz
在你的情况下,
<input type="hidden" checked="checked" name="1_WILLINFOS">//is an `input` type `hidden` with `attribute` checked with value `checked`
$('input[name=1_WILLINFOS]').attr('checked') == "checked" //is an element with name '1_WILLINFOS' and attribute checked set to 'checked' .So it will surely match and return true
但是,
.attr("checked") == true //will fail as you set `checked` attribute value to 'checked' and your are checked for true condition.so you must check for `.attr("checked") == 'checked`
$('input[name=1_WILLINFOS]').is(':checked'));// will check for attribute checked if its set it will return you true.
这是fiddle 为您工作