javascript - jQuery:文本并在单击时禁用它
问题描述
我想显示按钮的文本并在单击后将其禁用。
<button id="btnResendVerificationEmail" class="btn btn-success">click text</button>
<script type="text/javascript">
jQuery('#btnResendVerificationEmail').click(function() {
jQuery.post('clientarea.php', {
'token': '{$token}',
'action': 'resendVerificationEmail'
}).done(function(data) {
jQuery('#btnResendVerificationEmail').prop('disabled', true).text('done text');
});
});
</script>
解决方案
考虑使用jQuery.ajax()
可以使用beforeSend
回调的地方。这允许您在 HTTP Post 之前修改 Button。success
然后你可以在回调中再次修改它。
jQuery(function($) {
var int;
var c = 0;
function showLoading(obj) {
int = setInterval(function() {
var t = "Load";
switch (c % 3) {
case 0:
t += "...";
break;
case 1:
t += "..";
break;
case 2:
t += ".";
break;
}
c++;
obj.html(t);
}, 200);
}
$('#btnResendVerificationEmail').click(function() {
$.ajax({
url: "https://reqres.in/api/users",
data: {
token: "token-1234",
action: "resendVerficiationEmail"
},
method: "POST",
beforeSend: function() {
$('#btnResendVerificationEmail').prop("disabled", true);
showLoading($('#btnResendVerificationEmail'));
},
success: function(data) {
clearInterval(int);
$('#btnResendVerificationEmail').html("Done");
console.log(data);
}
});
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="btnResendVerificationEmail" class="btn btn-success">Send</button>
推荐阅读
- c# - 如何在 ASP.NET MVC 中连接两个模型?
- mongo-cxx-driver - 在 ubuntu 16.04 中安装 MongoDB C++ Driver 问题
- c++ - C++:此代码编译,但抛出运行时检查失败 #2 - 变量“num”周围的堆栈已损坏。发生了
- .htaccess - 如何在 htaccess 中停止 RewriteRule 否决 Redirect 301
- java - 部署战争。Tomcat 未考虑 META-INF 中的 context.xml
- javascript - 如何通过反应使 blueprintJs 对话框可拖动?
- mysql - MySQL:添加具有 NULL 值的列
- java - 如何从其他面板从 JTextField 获取输入
- sql - 导入当前日期年份表时面临的问题
- python - 如何在 python 中制作字典 - 列表结构?