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
Add a ment  | 

5 Answers 5

Reset to default 3

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

本文标签: