jQuery:检查禁用的属性并添加/删除它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14913845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:05:16  来源:igfitidea点击:

jQuery: Checking for disabled attribute and adding/removing it?

jqueryformsserializationattributes

提问by Sven

I select all input elements of a form like this:

我像这样选择表单的所有输入元素:

var fields = $( form + " :input" ).not( "button" );

How do I check if any of these inputs has the disabledattribute set and remove it when present?

如何检查这些输入中的任何一个是否disabled设置了属性并在存在时将其删除?

Also, I need to add it after is has been removed (and serialize the fields in between), is there an elegant way for this? Something like togglebut for attributes?

另外,我需要在删除后添加它(并序列化中间的字段),有没有一种优雅的方法?类似于toggle但对于属性?

回答by Ruben Infante

Assuming you are using jQuery 1.6 or above, the suggested method is to use .prop().

假设您使用 jQuery 1.6 或更高版本,建议的方法是使用.prop()

fields.prop("disabled", false);

fields.prop("disabled", false);

If you need to have logic around each one, you could do something to the extent of

如果您需要对每个人都有逻辑,您可以在以下范围内做一些事情

var fields = $( form + " :input" ).not( "button" );
fields.each(function(index, element) {
    var isDisabled = $(element).is(':disabled');
    if (isDisabled) {
        $(element).prop('disabled', false);
    } else {
        // Handle input is not disabled
    }
});

jsfiddle

提琴手

回答by glosrob

You can use the disabledselector

您可以使用disabled选择器

$('input:disabled').attr('disabled', '');

See here

这里

http://jsfiddle.net/JngR9/

http://jsfiddle.net/JngR9/

回答by Zubarik

To Remove

去除

$('input:disabled').removeProp('disabled');

or

或者

$('input:disabled').prop('disabled', 'disabled');

To Add

加上

$('input:disabled').prop('disabled', 'disabled');

回答by bipen

use :disabled Selector` try this

使用:disabled Selector` 试试这个

$('input:disabled').remove();