使用 jQuery 切换输入禁用属性 [英] Toggle input disabled attribute using jQuery
本文介绍了使用 jQuery 切换输入禁用属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码:
$("#product1 :checkbox").click(function(){
$(this)
.closest('tr') // Find the parent row.
.find(":input[type='text']") // Find text elements in that row.
.attr('disabled',false).toggleClass('disabled') // Enable them.
.end() // Go back to the row.
.siblings() // Get its siblings
.find(":input[type='text']") // Find text elements in those rows.
.attr('disabled',true).removeClass('disabled'); // Disable them.
});
如何切换.attr('disabled',false);
?
我似乎在 Google 上找不到.
I can't seem to find it on Google.
推荐答案
$('#el').prop('disabled', function(i, v) { return !v; });
.prop()
方法接受两个参数:
The .prop()
method accepts two arguments:
- 属性名称(已禁用、已选中、已选中)任何为真或假的内容
- 属性值,可以是:
- (empty) - 返回当前值.
- boolean (true/false) - 设置属性值.
- function - 为每个找到的元素执行,返回值用于设置属性.传递了两个参数;第一个参数是 index(0、1、2,每个找到的元素都会增加).第二个参数是元素的当前值(真/假).
- Property name (disabled, checked, selected) anything that is either true or false
- Property value, can be:
- (empty) - returns the current value.
- boolean (true/false) - sets the property value.
- function - Is executed for each found element, the returned value is used to set the property. There are two arguments passed; the first argument is the index (0, 1, 2, increases for each found element). The second argument is the current value of the element (true/false).
所以在这种情况下,我使用了一个函数,它为我提供了索引 (i) 和当前值 (v),然后我返回了当前值的相反值,因此属性状态被反转.
So in this case, I used a function that supplied me the index (i) and the current value (v), then I returned the opposite of the current value, so the property state is reversed.
这篇关于使用 jQuery 切换输入禁用属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文