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
jQuery: Checking for disabled attribute and adding/removing it?
提问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 disabled
attribute 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 toggle
but 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
}
});
回答by glosrob
回答by Zubarik
To Remove
去除
$('input:disabled').removeProp('disabled');
or
或者
$('input:disabled').prop('disabled', 'disabled');
To Add
加上
$('input:disabled').prop('disabled', 'disabled');