3

I've been trying to check if my input[type=hidden] has an checked="checked" attribute. That input field looks like this:

<input type="hidden" checked="checked" name="1_WILLINFOS">

In Firefox it works just fine by using:

jQuery(selector).find('input[name=1_WILLINFOS]').attr('checked') == "checked"

But IE10 always gives me a false.

By now I tried to use the following:

.attr("checked") == "checked"
.attr("checked") == true
.is(':checked')

With all of them giving me a false.

Any ideas? Thanks

4

2 回答 2

8

您不能将隐藏输入设为复选框,因此它不会有checked属性。如果您需要它是一个复选框,您可以使用 atype="checkbox"并隐藏它,或者您可以简单地使用带有 a或 a的隐藏输入来模拟它。display:none01

于 2013-09-07T17:38:21.280 回答
3

您可以拥有具有任何属性的隐藏元素。例如

<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. 

这是为你工作的小提琴

于 2013-09-07T18:00:53.143 回答