这是我的代码:
$("#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('禁用',假);?
我在谷歌上找不到。
过了一段时间,感谢@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)
我想要获得完整的浏览器可比性禁用应该设置的值禁用或得到删除!
这是我刚刚做的一个小插件:
(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);