英文:
javascript : Create a suitable function for loading the button?
问题
我有一个表单,我希望在表单按钮提交时在按钮上显示加载状态。我创建了这个模式,但它很简单。我想创建一个可以在所有表单中使用的函数,请帮助我,我不太懂jQuery?
const btnsabt = $('.btn-sabt');
btnsabt.addClass('submit-btn');
btnsabt.prop('disabled', true);
btnsabt.find('i').prop('hidden', false);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<form id="MobileUserForm" method="post">
<div class="form-group">
<input asp-for="Mobile" type="number" class="form-control" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-sabt">
<i class="fa fa-refresh fa-spin" hidden></i>保存
</button>
</div>
</form>
英文:
I have a form, I want a loading to be displayed on the button when the form button is submitted.
I created this mode but in a simple mode.
I want to create a function that I can use in all forms, please help me, I am not fluent in jQuery?
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const btnsabt = $('.btn-sabt');
btnsabt.addClass('submit-btn');
btnsabt.prop('disabled', true);
btnsabt.find('i').prop('hidden', false);
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<form id="MobileUserForm" method="post">
<div class="form-group">
<input asp-for="Mobile" type="number" class="form-control" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-sabt">
<i class="fa fa-refresh fa-spin" hidden></i>save
</button>
</div>
</form>
<!-- end snippet -->
答案1
得分: 1
Sure, here are the translated parts:
不确定 btnsabt.addClass('submit-btn');
是用来干什么的,如果需要,请取消注释
调用 toggleLoader(1); 来启动旋转,调用 toggleLoader(0); 来停止
任务理想情况下应该是异步的,因为界面需要更新
const toggleLoader = show => {
$('.btn-sabt')
// .toggleClass('submit-btn', show)
.prop('disabled', show)
.find('i').toggleClass('hide', !show)
};
$(function() {
$('#MobileUserForm').on('submit', (event) => {
event.preventDefault(); // 防止表单提交
toggleLoader(1); // 显示旋转器并禁用按钮
// 执行任务
// 完成后移除加载动画
// 用于显示延迟以可视化加载动画
setTimeout(() => { // 移除此行
toggleLoader(0); // 隐藏旋转器 - 在任务结束时调用
}, 3000); // 移除此行
});
});
.hide{
display: none !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<form id="MobileUserForm" method="post">
<div class="form-group">
<input asp-for="Mobile" type="number" class="form-control" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-sabt">
<i class="fa fa-refresh fa-spin hide"></i> 保存
</button>
</div>
</form>
英文:
Not sure what the btnsabt.addClass('submit-btn');
is for, if needed uncomment it
Call toggleLoader(1); to spin and toggleLoader(0); to stop
the task should ideally be async since the interface needs to update
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const toggleLoader = show => {
$('.btn-sabt')
// .toggleClass('submit-btn',show)
.prop('disabled', show)
.find('i').toggleClass('hide',!show)
};
$(function() {
$('#MobileUserForm').on('submit', (event) => {
event.preventDefault(); //prevent form submission
toggleLoader(1); // show the spinner and disable
//perform tasks
//after completion, remove the loading
//test to show a delay to visualise the loader
setTimeout(() => { // remove this line
toggleLoader(0); // hide the spinner - call this at the end of the task
}, 3000); //remove this line
});
});
<!-- language: lang-css -->
.hide{
display: none !important;
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<form id="MobileUserForm" method="post">
<div class="form-group">
<input asp-for="Mobile" type="number" class="form-control" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-sabt">
<i class="fa fa-refresh fa-spin hide"></i> save
</button>
</div>
</form>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论