这是我的代码:

$("#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上找到它。

有帮助吗?

解决方案

$('#el').prop('disabled', function(i, v) { return !v; });

.prop() 方法接受两个参数:

  • 财产 姓名 (禁用,检查,选择)任何是真或错误
  • 财产 价值, , 可:
    • (空的) - 返回当前值。
    • 布尔 (true/fals) - 设置属性值。
    • 功能 - 为每个发现的元素执行,返回值用于设置属性。有两个论点。第一个论点是 指数 (0、1、2,每个发现的元素增加)。第二个论点是电流 价值 元素(true/false)。

因此,在这种情况下,我使用了为我提供索引(i)和当前值(v)的函数,然后返回了当前值的相反,因此属性状态被逆转。

其他提示

猜测 获得完整的浏览器可比性 disabled 应由值设置 disabled 或被删除!
这是我刚刚制作的一个小插件:

(function($) {
    $.fn.toggleDisabled = function() {
        return this.each(function() {
            var $this = $(this);
            if ($this.attr('disabled')) $this.removeAttr('disabled');
            else $this.attr('disabled', 'disabled');
        });
    };
})(jQuery);

示例链接.

编辑:将示例链接/代码更新到维护链性!
编辑2:
基于@lonesomeday评论,这是一个增强版本:

(function($) {
    $.fn.toggleDisabled = function(){
        return this.each(function(){
            this.disabled = !this.disabled;
        });
    };
})(jQuery);
    $('#checkbox').click(function(){
        $('#submit').attr('disabled', !$(this).attr('checked'));
    });

一段时间后,感谢@Arne,我创建了这个相似的小函数,以处理应禁用和隐藏或启用并显示的输入的地方:

function toggleInputState(el, on) {
  // 'on' = true(visible) or false(hidden)
  // If a field is to be shown, enable it; if hidden, disable it.
  // Disabling will prevent the field's value from being submitted
  $(el).prop('disabled', !on).toggle(on);
}

然后,简单地使用以下方式切换一个jQuery对象(例如$('input [name =“ something”]))。

toggleInputState(myElement, myBoolean)

单击复选框时更新单击的另一个简单选项。

html:

<input type="checkbox" id="checkbox/>
<input disabled type="submit" id="item"/>

jQuery:

$('#checkbox').click(function() {
    if (this.checked) {
        $('#item').prop('disabled', false); // If checked enable item       
    } else {
        $('#item').prop('disabled', true); // If checked disable item                   
    }
});

在行动中: 关联

使用回调语法非常简单 attr:

$("#product1 :checkbox").click(function(){
  $(this)
   .closest('tr') // find the parent row
       .find(":input[type='text']") // find text elements in that row
           .attr('disabled',function(idx, oldAttr) {
               return !oldAttr; // invert disabled value
           })
           .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',function(idx, oldAttr) {
                   return !oldAttr; // invert disabled value
               })
               .removeClass('disabled'); // disable them
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top