Disabling an element in jquery 1.6

2019-04-21 00:11发布

With the release of jQuery 1.6, the recommendation on SO has been to generally start using prop() where you used to use attr().

What happens when I want to disable an element?

$('.control').prop('disabled', 'disabled');
$('.control').prop('disabled', true);

Neither of these seem to disable the control. Is disabling an element the exception to the rule?

UPDATE And turns out the reason the element wasn't being disabled was because of a line I had that ran before the lines above:

$('.control').removeProp('disabled');

In enabling controls, I had got used to using .removeAttr() so figured .removeProp would be enough. Instead, use the following to enable controls:

$('.control').prop('disabled', false);

标签: jquery-1.6
3条回答
劫难
2楼-- · 2019-04-21 00:51

Make sure you haven't .removeProp('disabled') on the element you're trying to disable.

查看更多
趁早两清
3楼-- · 2019-04-21 00:55
$('.control').prop('disabled', true);

works fine for me.

Alternatively use

$('.control').attr('disabled', 'disabled');

Update:

But even $('.control').prop('disabled', 'disabled'); or $('.control').attr('disabled', true); disable the element too. So if it does not work for you, the question is really which element you are trying to disable. Can it be even disabled?

查看更多
Viruses.
4楼-- · 2019-04-21 01:04

Use .prop('disabled', true).

http://jsfiddle.net/mattball/BaLHf/

查看更多
登录 后发表回答