使用jQuery暂时禁用按钮 [英] Temporarily disable button using jQuery
本文介绍了使用jQuery暂时禁用按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在不删除监听器的情况下禁用按钮元素,例如,我有以下代码:
I would like to disable a button element without removing the listener, for example I have ths following code:
<input id="in" />
<button id="sub">Submit</button>
$('#sub').click(function (e) {
//Some actions
});
$($('#in').keyup(function (e) {
if (new Date($(this).val()) == 'Invalid Date') {
$(this).addClass('invalid');
$('#sub').addClass('disabled');
}
else {
$(this).removeClass('invalid');
$('#sub').removeClass('disabled');
}
});
我想取消绑定按钮单击侦听器,但是如果我要使用off()
或unbind()
,则必须在else
子句中对其进行重新绑定".
I would like to unbind the button click listener, but if I'll use off()
or unbind()
I will have to 're-bind' it in the else
clause.
还有更好的方法吗?
推荐答案
如何禁用按钮而不添加类?
How about disabling the button instead of adding a class?
HTML:
<button>Disable Me</button>
JS
$(function() {
$("button").click(function() {
alert("click!");
$(this).prop("disabled", true);
});
});
CSS
button[disabled] {
color: red;
}
此处是要演示的jsFiddle.
Here is a jsFiddle to demonstrate.
这篇关于使用jQuery暂时禁用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文