【问题标题】:Toggle input disabled attribute using jQuery使用 jQuery 切换输入禁用属性
【发布时间】:2011-01-15 20:37:21
【问题描述】:

这是我的代码:

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

【问题讨论】:

  • 有什么理由不能使用该字段的 disabled 属性? $("input").get(0).disabled = isFalse; // jsfiddle.net/uAfhj
  • 我找到了DependsOn 插件,您可能会觉得它很有用

标签: jquery toggle attr


【解决方案1】:
$('#el').prop('disabled', function(i, v) { return !v; });

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

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

所以在这种情况下,我使用了一个函数,它为我提供了索引 (i) 和当前值 (v),然后我返回了与当前值相反的值,所以属性状态是相反的。

【讨论】:

  • 赞成(我相信).prop() 是正确的方法,并且正是为了设置 disabled="disabled" + 它的优雅而添加的。
  • 这是一个很好的答案!
  • 完美的简单、简洁、有效的解决方案!谢谢老兄!
  • 很好的解决方案。
  • 作为更新,使用箭头函数看起来非常整洁:$('#el').prop('disabled', (i, v) => !v);
【解决方案2】:

要获得完整的浏览器可比性 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);

Example link

编辑:更新了示例链接/代码以保持可链接性!
编辑 2:
基于@lonesomeday 评论,这是一个增强版:

(function($) {
    $.fn.toggleDisabled = function(){
        return this.each(function(){
            this.disabled = !this.disabled;
        });
    };
})(jQuery);

【讨论】:

  • 这可能有效,但会很慢,因为您不断地创建新的 jQuery 对象。 $.fn.toggleDisabled = function(){ return this.each(function(){ this.disabled = !this.disabled; });} 就是你所需要的。
  • @lonesomeday:我打算发布这个,但我倾向于认为这不是设置/取消设置disabled 属性的正确方法。无论如何,如果您可以确认这是一个跨浏览器解决方案..我会更新我的答案。
  • 对于任何未来的谷歌员工,此解决方案同样适用于“必需”属性。
  • prop 是 Arne 所说的自 1.6 以来更好的方法。
  • 不,我不建议这样做,道具更好
【解决方案3】:
$('#checkbox').click(function(){ $('#submit').attr('disabled', !$(this).attr('checked')); });

【讨论】:

  • 注意:仅适用于 jQuery 1.6 以下。在两次出现时使用 .prop() 而不是 attr() 来获取布尔值。见api.jquery.com/prop
  • @ManuelArwedSchmidt 不对。认为您还不了解attrprop 之间的区别。
【解决方案4】:

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

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

实际操作中:link

【讨论】:

  • $('#item').prop('disabled', this.checked);去掉if
【解决方案5】:

过了一会儿,多亏了@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)

【讨论】:

    【解决方案6】:

    这在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
    });
    

    【讨论】:

      猜你喜欢
      • 2012-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-26
      • 2015-12-14
      • 1970-01-01
      • 2021-11-16
      • 1970-01-01
      相关资源
      最近更新 更多