这是我的代码:

$("#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('禁用',假);?

我在谷歌上找不到。


当前回答

我想要获得完整的浏览器可比性禁用应该设置的值禁用或得到删除! 这是我刚刚做的一个小插件:

(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'));
    });

使用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
});

我想要获得完整的浏览器可比性禁用应该设置的值禁用或得到删除! 这是我刚刚做的一个小插件:

(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);
$('#el').prop('disabled', (i, v) => !v);

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

属性名(禁用,选中,选中)为真或假的任何值 属性值,可以为: (空)-返回当前值。 Boolean (true/false) -设置属性值。 function -对每个找到的元素执行,返回值用于设置属性。传递了两个参数;第一个参数是索引(0,1,2为每个找到的元素增加)。第二个参数是元素的当前值(true/false)。

因此,在本例中,我使用了一个函数,它为我提供了索引(I)和当前值(v),然后我返回了与当前值相反的值,因此属性状态颠倒了。

过了一段时间,感谢@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)