简体   繁体   English

使用jQuery我们如何从值为false的所有元素中删除disabled属性?

[英]Using jQuery how can we remove disabled attribute from all elements whose value is false?

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. 我知道disabled属性没有任何价值。 So when the attribute is present the element becomes disabled irrespective of the value assigned to it. 因此,当属性存在时,无论分配给它的值如何,元素都将被禁用。 I want to remove all the disabled attribute from all input elements whose value is false. 我想从值为false的所有输入元素中删除所有disabled属性。

Using jQuery I would like to use code like the following. 使用jQuery我想使用如下代码。

$("*[disabled]").not(true).removeAttr("disabled");

Why don't you just match elements where disabled is false ? 为什么不匹配disabledfalse元素?

$('[disabled="false"]​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​')​.removeAttr('disabled');​​​

Demo: http://jsfiddle.net/ASN29/ 演示: http//jsfiddle.net/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. 无论属性的值如何, disabled属性的存在都会自动disabled该元素,因此这不是一个好主意。 How does the HTML become this way? HTML如何变成这种方式?

You could simply use a selector like this: 您可以简单地使用这样的选择器:

$('input[disabled="false"]').removeAttr("disabled");

jsFiddle Demo jsFiddle演示

Instead of * , I added input , it narrows the query down a lot. 而不是* ,我添加了input ,它缩小了查询范围。 This will search for input elements that have an attribute called disabled having the value false . 这将搜索具有名为disabled的属性的input元素,其值为false

I would suggest you don't do this at all. 我建议你根本不要这样做。 That HTML should have never been generated. 应该从未生成过该HTML。

If you want to enabled them use the .prop() instead .. ( that is because disabled as an actual property of checkbox inputs ) 如果你想启用它们,请使用.prop()代替..( 因为disabledcheckbox输入的实际属性

$('input[disabled="false"]').prop('disabled', false);

Demo at http://jsfiddle.net/gaby/Bn4dr/ 演示http://jsfiddle.net/gaby/Bn4dr/


The correct way, though, would be to print the proper html directly 但是,正确的方法是直接打印正确的html

<input type="checkbox" id="a1" value="11">
<input type="checkbox" id="a2" value="21" disabled>
<input type="checkbox" id="a3" value="31">
​$(document).ready(function (){
    $('input:disabled').removeAttr('disabled');    
});​

Here is the jsFiddle for it and documentation on :disabled selector 以下是jsFiddle及其文档:禁用选择器

$('input[disabled="false"]').removeAttr("disabled");

See http://jsfiddle.net/JKs4C/ http://jsfiddle.net/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. 否则,如果您的脚本出错,noscript等,您将最终禁用所有元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM