[英]Jquery - multi-step form progress bar using unordered list items to show completed steps
我正在嘗試使用JQuery構建多步驟表單,但是進度欄存在一些問題。
我的進度欄是一個無序列表,其中的列表項在用戶按下下一步時應以紅色突出顯示。 但是,當他們按上一個列表項時,應該會丟失突出顯示的紅色。 我非常接近解決方案。 我有前兩個選項,但是當我轉到最后一步時,沒有看到任何突出顯示。
這是我的小提琴: http : //jsfiddle.net/ktLcfzhe/
jQuery的:$(document).ready(function(){
var current_fs = $('.current'); //current fieldset
$('.next').click(function () {
$('.current').removeClass('current').hide().next().show().addClass('current');
if ($(this).parent('.field1')) {
$('#progressbar li').next('.second').addClass('active');
} else if ($(this).parent('.field2')) {
$('#progressbar li').next('.last').addClass('active');
} else if ($(this).parent('.field3')) {
$('#progressbar li').addClass('active');
}
});
$('.previous').click(function () {
$('.current').removeClass('current').hide().prev().show().addClass('current');
if ($(this).parent('.field3')) {
$('#progressbar li').prev('.last').removeClass('active');
} else if ($(this).parent('.field2')) {
$('#progressbar li').prev('.second').removeClass('active');
}
});
});
HTML:
<form id="helpdeskform" action="process.php" method="post">
<!-- Progress Bar -->
<ul id="progressbar">
<li class="active first">Identify Yourself</li>
<li class="second">Describe Request</li>
<li class="last">Confirm and Submit</li>
</ul>
<fieldset class="field1 current">
<h2>Identify Yourself</h2>
<p>
<label for="fname">
<input type="text" value="" name="" id="" placeholder="First Name" />
</label>
</p>
<p>
<label for="lname">
<input type="text" value="" name="" id="" placeholder="Last Name" />
</label>
</p>
<p>
<label for="Next">
<input type="button" name="next" class="next action-button" value="Next" />
</label>
</p>
</fieldset>
<fieldset class="field2">
<h2>Describe Request</h2>
<p>
<label for="">
<input type="text" value="" name="" id="" placeholder="Subject" />
</label>
</p>
<p>
<label for="">
<textarea style="font-family: Arial, Veranda, Sans serif" name="" id="" cols="30" rows="10"></textarea>
</label>
</p>
<p style="float:left;">
<label for="previous">
<input type="button" name="previous" class="previous action-button" value="Previous" />
</label>
</p>
<p style="float:left; padding-left: 10px;">
<label for="Next">
<input type="button" name="next" class="next action-button" value="Next" />
</label>
</p>
</fieldset>
<fieldset class="field3">
<h2>Confirm and Submit</h2>
<p>
<label for="fname">
<input type="text" value="" name="" id="" placeholder="" />
</label>
</p>
<p>
<label for="">
<input type="text" value="" name="" id="" placeholder="" />
</label>
</p>
<p>
<label for="">
<input type="text" value="" name="" id="" placeholder="" />
</label>
</p>
<p>
<label for="">
<input type="text" value="" name="" id="" placeholder="" />
</label>
</p>
<p style="float:left;">
<label for="previous">
<input type="button" name="previous" class="previous action-button" value="Previous" />
</label>
</p>
<p style="float:left; padding-left: 10px;">
<label for="Submit">
<input type="button" value="Submit" name="submit" id="submit" />
</label>
</p>
</fieldset>
</form>
CSS:
/*form styles*/
#helpdeskform {
text-align: center;
position: relative;
}
/*Hide all except first fieldset*/
#helpdeskform .field2, .field3 {
display: none;
}
/*inputs*/
#helpdeskform input, #helpdeskform textarea {
padding: 15px;
border: 1px solid #ccc;
border-radius: 3px;
margin-bottom: 10px;
width: 100%;
box-sizing: border-box;
color: #2C3E50;
font-size: 13px;
}
/*buttons*/
#helpdeskform .action-button {
width: 100px;
font-weight: bold;
border: 0 none;
border-radius: 1px;
cursor: pointer;
padding: 10px 5px;
margin: 10px 5px;
}
#helpdeskform .action-button:hover, #msform .action-button:focus {
box-shadow: 0 0 0 2px white, 0 0 0 3px #900;
}
/*progressbar*/
#progressbar {
margin-bottom: 30px;
overflow: hidden;
counter-reset: step;
/*CSS counters to number the steps*/
}
#progressbar li {
list-style-type: none;
text-transform: uppercase;
font-size: 10px;
width: 30%;
float: left;
position: relative;
}
#progressbar li:before {
content: counter(step);
counter-increment: step;
width: 20px;
line-height: 20px;
display: block;
font-size: 10px;
color: #333;
background: white;
border-radius: 3px;
margin: 0 auto 5px auto;
}
/*progressbar connectors*/
#progressbar li:after {
content:'';
width: 100%;
height: 2px;
background: white;
position: absolute;
left: -50%;
top: 9px;
z-index: -1;
/*put it behind the numbers*/
}
#progressbar li:first-child:after {
content: none;
/*connector not needed before the first step*/
}
/*marking active/completed steps nhlbi red*/
/*The number of the step and the connector before it = red*/
#progressbar li.active:before, #progressbar li.active:after {
background: #900;
color: white;
}
所有評論表示贊賞。 我使用http://thecodeplayer.com/walkthrough/jquery-multi-step-form-with-progress-bar作為此表單的指南。 那里的JQuery對我來說太復雜了。
這是一個工作示例http://jsfiddle.net/ktLcfzhe/1/
jQuery的
$('.next').click(function () {
$('.current').removeClass('current').hide().next().show().addClass('current');
$('#progressbar li.active').next().addClass('active');
});
$('.previous').click(function () {
$('.current').removeClass('current').hide().prev().show().addClass('current');
$('#progressbar li.active').removeClass('active').prev().addClass('active');
});
這是您的解決方法: http : //jsfiddle.net/csdtesting/ktLcfzhe/2/
$(document).ready(function () {
$('.next').click(function () {
$('.current').removeClass('current').hide().next().show().addClass('current');
$('#progressbar li.active').next().addClass('active');
if ($('#progress')) {};
});
$('.previous').click(function () {
$('.current').removeClass('current').hide().prev().show().addClass('current');
$('#progressbar li.active').removeClass('active').prev().addClass('active');
});
});
我將通過在更高級別上添加一個類來描述用戶所處的步驟來進行處理。
從#progressbar上的“第一步”類開始。 當用戶單擊下一步時,添加一個名為“第二步”的類(不要刪除“第一步”)。 當他們再次單擊下一步時,添加第三類“第三步”。
然后在您的CSS中,使用如下規則:
#progressbar.step-one .first {
//show me highlighted
}
#progressbar.step-two .second {
//show me highlighted
}
#progressbar.step-three .third {
//show me highlighted
}
如果為接下來的按鈕提供一個ID,該ID是要應用於進度欄的類,則可以在一個單擊事件中處理所有這些事件。
例如,如果第一個下一個按鈕如下所示:
<label for="Next">
<input id="go-to-step-two" type="button" name="next" class="next action-button" value="Next" />
</label>
然后,您的JavaScript可能如下所示:
$('.next').click(function(e) {
var currentPage = this.attr('id').replace('go-to-', ''); //e.g. 'step-two'
$('#progressbar').class(currentPage);
});
對后退按鈕使用相同的邏輯,只是根據后退按鈕的ID刪除類。
希望能有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.