jquery - 进度条上的返回按钮
问题描述
我一直在尝试使用 BACK & NEXT 按钮创建多步骤内容结构。一切都按预期工作,但后退按钮没有从进度条中删除活动类。任何人都可以看看我的代码和帮助。
$(document).ready(function(){
//steps ui
var progressBar = {
Bar : $('#progress-bar'),
Reset : function(){
},
Next: function(){
$('#progress-bar li:not(.active):first').addClass('active');
},
Back: function(){
$('#progress-bar li.active:last').removeClass('active');
}
}
var current = 1;
widget = $(".step");
btnnext = $(".next");
btnback = $(".back");
btnsubmit = $(".submit");
// Init buttons and UI
widget.not(':eq(0)').hide();
hideButtons(current);
setProgress(current);
// Next button click action
btnnext.click(function(){
if(current < widget.length) {
widget.show();
widget.not(':eq('+(current++)+')').hide();
setProgress(current);
//alert("I was called from btnNext");
}
hideButtons(current);
progressBar.Next();
});
// Back button click action
btnback.click(function(){
if(current > 1){
current = current - 2;
btnnext.trigger('click');
}
hideButtons(current);
progressBar.Back();
});
});
附上下面的codepen链接
解决方案
There is a small mistake in your code. When you click on back button, next button clicked is trigger. Please update your btnback click function with following
$(document).ready(function() {
//steps ui
var progressBar = {
Bar: $('#progress-bar'),
Reset: function() {
},
Next: function() {
$('#progress-bar li:not(.active):first').addClass('active');
},
Back: function() {
$('#progress-bar li.active:last').removeClass('active');
}
}
var current = 1;
widget = $(".step");
btnnext = $(".next");
btnback = $(".back");
btnsubmit = $(".submit");
// Init buttons and UI
widget.not(':eq(0)').hide();
hideButtons(current);
setProgress(current);
// Next button click action
btnnext.click(function() {
if (current < widget.length) {
widget.show();
widget.not(':eq(' + (current++) + ')').hide();
setProgress(current);
//alert("I was called from btnNext");
}
hideButtons(current);
progressBar.Next();
});
// Back button click action
btnback.click(function(event) {
var divShowCount = 0
if (current > 1) {
current = current - 1;
divShowCount = current - 1
}
widget.show();
widget.not(':eq(' + (divShowCount) + ')').hide();
hideButtons(current);
progressBar.Back();
});
});
// Change progress bar action
setProgress = function(currstep) {
var percent = parseFloat(100 / widget.length) * currstep;
percent = percent.toFixed();
$(".progress-bar")
.css("width", percent + "%")
.html(percent + "%");
}
// Hide buttons according to the current step
hideButtons = function(current) {
var limit = parseInt(widget.length);
$(".action").hide();
if (current < limit)
btnnext.show();
if (current > 1) btnback.show();
if (current == limit) {
btnnext.hide();
btnsubmit.show();
}
}
I hope this helps you
推荐阅读
- python - 需要以下格式的 Python 熊猫
- jestjs - 使用 Jest 测试 NestJS + TypeORM 时“'instanceof' 的右侧不是对象”
- docker - 如何通过 cli 检查 Docker Swarm 资源预留
- python - 为什么 ipywidgets 的小部件没有在 html 文件中定义?
- c++ - 使用 Playsound c++ 在后台运行音乐
- spring-boot - Spring boot oauth2 客户端凭据使用 webflux 动态配置
- python - 切片列表以提取给定索引的最后 k 和下 k 元素
- reactjs - 在 Docker 上运行 CRA 时出现错误(0,_schemaUtils.default)不是函数
- r - 将一个字符添加到 R 中向量列表中的每个向量
- node.js - 如何将数组添加到存在于 mongodb 对象中的另一个数组中