开发者

strange issue with jQuery

I have a very strange issue with jQuery.

The problem:

$("#button-prev").attr("disabled", "disabled");

This doesn't work. If I check with Firebug I see disabled="" as element attribute

But for example this

$("#button开发者_StackOverflow社区-prev").attr("disablea", "disabled");

works as you should expect. Anyone an idea what it could be?


The disabled is a boolean property. Therefore it's just enough if a truthy value is set.

<button disabled>foo</button>

would work aswell in most browsers. You need to either completely remove the property by using

$('#button-prev').removeAttr('disabled');

or set it explicitly to false / 0

$('#button-prev').attr('disabled', false);

It's a common mistake and pretty confusing, consider, a call like

$(element).attr('disabled', 'false');

would also disable the element. Since you passed in a string value which is a truthy value.

Further info: http://www.w3.org/TR/html401/interact/forms.html#h-17.12.1


Have you tried, if the button is disabled? I just tried it with the "Post Your Answer" button on this page, and although Firebug doesn't show anything in the HTML tab, the DOM tab reveals, that the disabled attribute is set (and behold, I can't click).


I believe the attr for that is "true" or "false"... Try: $("#button-prev").attr("disabled", true); Make sure "button-prev" is the ID for the button.


In plain ol' JavaScript, it's a simple as

document.getElementById('button-prev').disabled = true;

This is one of the few occassions where I think it would be easier not to use jQuery

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜