使用jQuery切换输入禁用属性 [英] Toggle input disabled attribute using jQuery

查看:73
本文介绍了使用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:

  • 属性名称(禁用,选中,选中)任何为真或为假
  • 属性可以是:
    • ()-返回当前值.
    • 布尔值(是/否)-设置属性值.
    • 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆