Tom*_*old 177 jquery toggle attr
这是我的代码:
$("#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.
});
Run Code Online (Sandbox Code Playgroud)
我如何切换.attr('disabled',false);
?
我似乎无法在Google上找到它.
Arn*_*rne 421
$('#el').prop('disabled', function(i, v) { return !v; });
Run Code Online (Sandbox Code Playgroud)
该.prop()
方法接受两个参数:
所以在这种情况下,我使用了一个为索引(i)和当前值(v)提供的函数,然后我返回了与当前值相反的函数,因此属性状态被反转.
ifa*_*our 101
我想获得完整的浏览器的可比性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);
Run Code Online (Sandbox Code Playgroud)
示例链接.
编辑:更新示例链接/代码以保持可链接性!
编辑2:
基于@lonesomeday评论,这是一个增强版本:
(function($) {
$.fn.toggleDisabled = function(){
return this.each(function(){
this.disabled = !this.disabled;
});
};
})(jQuery);
Run Code Online (Sandbox Code Playgroud)
小智 19
$('#checkbox').click(function(){ $('#submit').attr('disabled', !$(this).attr('checked')); });
小智 7
另一个简单的选项,单击复选框即可更新。
HTML:
<input type="checkbox" id="checkbox/>
<input disabled type="submit" id="item"/>
Run Code Online (Sandbox Code Playgroud)
jQuery:
$('#checkbox').click(function() {
if (this.checked) {
$('#item').prop('disabled', false); // If checked enable item
} else {
$('#item').prop('disabled', true); // If checked disable item
}
});
Run Code Online (Sandbox Code Playgroud)
在行动:链接
归档时间: |
|
查看次数: |
166149 次 |
最近记录: |