admin管理员组文章数量:1277406
I have input checkboxes printed like the following.
<input type="checkbox" id="a1" value="11" disabled="false">
<input type="checkbox" id="a2" value="21" disabled="true">
<input type="checkbox" id="a3" value="31" disabled="false">
I know the disabled attribute takes no value. So when the attribute is present the element bees disabled irrespective of the value assigned to it. I want to remove all the disabled attribute from all input elements whose value is false.
Using jQuery I would like to use code like the following.
$("*[disabled]").not(true).removeAttr("disabled");
I have input checkboxes printed like the following.
<input type="checkbox" id="a1" value="11" disabled="false">
<input type="checkbox" id="a2" value="21" disabled="true">
<input type="checkbox" id="a3" value="31" disabled="false">
I know the disabled attribute takes no value. So when the attribute is present the element bees disabled irrespective of the value assigned to it. I want to remove all the disabled attribute from all input elements whose value is false.
Using jQuery I would like to use code like the following.
$("*[disabled]").not(true).removeAttr("disabled");
Share
Improve this question
edited Dec 21, 2012 at 0:10
kapa
78.7k21 gold badges165 silver badges178 bronze badges
asked Dec 21, 2012 at 0:00
dipudipu
1,3403 gold badges16 silver badges28 bronze badges
5
- Something to note: once this is removed, AFAIK, it cannot be put back. – Sparky Commented Dec 21, 2012 at 0:06
- 1 But why would you want to do this? – kapa Commented Dec 21, 2012 at 0:09
- Exactly... why even do this? – Sparky Commented Dec 21, 2012 at 0:10
- 3 Fix the problem at source. Don't deliver the broken HTML to the client in the first place. – Quentin Commented Dec 21, 2012 at 0:10
- I am using a cloud based development framework where I cannot print attribute in an element with no value. So I am going to use jQuery to remove the one with false value. – dipu Commented Dec 21, 2012 at 1:02
5 Answers
Reset to default 3Why don't you just match elements where disabled
is false
?
$('[disabled="false"]').removeAttr('disabled');
Demo: http://jsfiddle/ASN29/
The presence of the disabled
attribute automatically makes the element disabled, regardless of the attribute's value, so this isn't a very good idea. How does the HTML bee this way?
If you want to enabled them use the .prop()
instead .. (that is because disabled
as an actual property of checkbox
inputs)
$('input[disabled="false"]').prop('disabled', false);
Demo at http://jsfiddle/gaby/Bn4dr/
The correct way, though, would be to print the proper html directly
<input type="checkbox" id="a1" value="11">
<input type="checkbox" id="a2" value="21" disabled>
<input type="checkbox" id="a3" value="31">
You could simply use a selector like this:
$('input[disabled="false"]').removeAttr("disabled");
jsFiddle Demo
Instead of *
, I added input
, it narrows the query down a lot. This will search for input
elements that have an attribute called disabled
having the value false
.
I would suggest you don't do this at all. That HTML should have never been generated.
$(document).ready(function (){
$('input:disabled').removeAttr('disabled');
});
Here is the jsFiddle for it and documentation on :disabled selector
$('input[disabled="false"]').removeAttr("disabled");
See http://jsfiddle/JKs4C/
As to whether this is a good idea, on the other hand... I think the real answer is to fix up your server-side rendering to omit the disabled attribute entirely for elements that aren't disabled. Otherwise you'll end up with all elements disabled if your scripts go wrong, noscript, etc.
本文标签:
版权声明:本文标题:javascript - Using jQuery how can we remove disabled attribute from all elements whose value is false? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741287751a2370370.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论