当AJAX调用完成时重置Bootstrap加载状态按钮
Reset Bootstrap loading state button when AJAX call is completed
我有一个简单的AJAX请求,我想知道是否有可能将加载状态按钮与我的请求相结合。是否有可能使按钮重置为默认值时,请求完成,而不是选择例如5秒前重置为现在?
加载状态按钮
$("button").click(function() {
var $btn = $(this);
$btn.button('loading');
// simulating a timeout
setTimeout(function () {
$btn.button('reset');
}, 1000);
});
AJAX请求
$(function () {
$('form').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'POST',
dataType:'html',
url: '/m/core/_processEditEntry.php',
data: $('form').serialize(),
success: function () {
$(".message").fadeIn(0);
$(".message").delay(5000).fadeOut('slow');
}
});
});
});
最好将代码保存在同一个地方。删除单击处理程序并将以下行添加到ajax调用中:
$(function () {
$('form').on('submit', function (e) {
//save button so we can use later
var my_button = $(this).find("button");
//give button loading state
my_button.button('loading');
e.preventDefault();
$.ajax({
type: 'POST',
dataType:'html',
url: '/m/core/_processEditEntry.php',
data: $('form').serialize(),
success: function () {
//reset state
my_button.button('reset');
$(".message").fadeIn(0);
$(".message").delay(5000).fadeOut('slow');
}
});
});
});
当按钮被点击时,你可以添加一个带有类的span
if ($(this).is(".btn, .page-link, .df-link")) {
// disable button
$(".btn").prop("disabled", true);
// add spinner to button
$(this).html(
`<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> ` + $(this).text()
);
}
,然后在Ajax中完全删除该元素:
complete: function(response) {
$(".btn").prop("disabled", false);
$(".spinner-border").remove();
}
相关文章:
- Javascript+Bootstrap图像库未加载
- 使用Bootstrap将JSON文件加载到表中
- Bootstrap在页面加载缓慢时会立即显示隐藏的模式对话框
- 如何实现 ladda-bootstrap 按钮加载动画
- Bootstrap JS模式加载,但不可编辑
- Bootstrap在加载时崩溃
- 如何通过不同的方式从控制器加载ui.bootstrap模块
- 如果通过ajax加载,如何在bootstrap中打开模态
- Twitter Bootstrap js文件已加载,但无法与requirejs一起使用
- 需要在bootstrap css中单击时将图像加载为模态
- 如何激活 bootstrap.tagsinput 用于 DOM 加载后添加的输入
- 如何防止/延迟一堆 Bootstrap 模块立即加载其内容以防止站点加载时间缓慢
- 让 Bootstrap datetimepicker 在 ajax 加载后工作
- Twitter-bootstrap Modal 未显示在页面加载上(灰屏)
- 谷歌地图图块未在Bootstrap-Modal中加载
- Bootstrap Tour不会在Modal上加载
- 如何在加载页面然后隐藏页面时显示 Bootstrap 3 弹出框
- 在重新加载代码段上设置 Bootstrap 当前选项卡,而不是在本地存储中存储最后一个选项卡
- Bootstrap模式加载远程内容,生成2个请求
- 当AJAX调用完成时重置Bootstrap加载状态按钮