gpt4 book ai didi

jquery - 多步骤表单验证jquery

转载 作者:行者123 更新时间:2023-12-01 04:46:33 25 4
gpt4 key购买 nike

我有以下多步骤表格,

   <form id="msform">
<!-- progressbar -->
<ul id="progressbar">
<li class="active">Account Setup</li>
<li>Social Profiles</li>
<li>Personal Details</li>
</ul>
<!-- fieldsets -->

<fieldset id="form_1">
<h2 class="fs-title">Personal Details</h2>
<h3 class="fs-subtitle">Step 1</h3>
<input type="text" name="firstname" placeholder="First Name" />
<input type="text" name="lastname" placeholder="Last Name" />
<input type="text" name="email" id="email" placeholder="Email Address" />
<input type="button" name="next" id="next_btn1" class="next action-button" value="Next" />
</fieldset>
<fieldset id="form_2">
<h2 class="fs-title">More Details</h2>
<h3 class="fs-subtitle">Step 2</h3>
<input type="text" name="Phone" placeholder="Phone" />
<input type="text" id="dob" name="DOB" placeholder="Date of Birth" />
<input type="text" name="gender" placeholder="Gender" />
<input type="button" name="previous" class="previous action-button" value="Previous" />
<input type="button" name="next" class="next action-button" value="Next" />
</fieldset>
<fieldset id="form_3">
<h2 class="fs-title">Create Your Account</h2>
<h3 class="fs-subtitle">Step 3</h3>
<input type="text" name="username" id="username" placeholder="UserName" />
<input type="password" name="password" placeholder="password" />
<input type="password" name="passwordR" placeholder="Confirm Password" />
<input type="button" name="previous" class="previous action-button" value="Previous" />
<input type="submit" name="submit" class="submit action-button" value="Submit" />

</fieldset>
</form>

我想使用验证插件向此表单添加 jquery 验证。如果验证有效,则只能进入下一步,如果进入上一步,则重置字段值。这是 jquery 代码,

var current_fs, next_fs, previous_fs; 
var left, opacity, scale;
var animating;

$(".next").click(function(){
if(animating) return false;
animating = true;

current_fs = $(this).parent();
next_fs = $(this).parent().next();

//activate next step on progressbar using the index of next_fs
$("#progressbar li").eq($("fieldset").index(next_fs)).addClass("active");

//show the next fieldset
next_fs.show();
//hide the current fieldset with style
current_fs.animate({opacity: 0}, {
step: function(now, mx) {
//as the opacity of current_fs reduces to 0 - stored in "now"
//1. scale current_fs down to 80%
scale = 1 - (1 - now) * 0.2;
//2. bring next_fs from the right(50%)
left = (now * 50)+"%";
//3. increase opacity of next_fs to 1 as it moves in
opacity = 1 - now;
current_fs.css({'transform': 'scale('+scale+')'});
next_fs.css({'left': left, 'opacity': opacity});
},
duration: 800,
complete: function(){
current_fs.hide();
animating = false;
},
//this comes from the custom easing plugin
easing: 'easeInOutBack'
});
});

$(".previous").click(function(){
if(animating) return false;
animating = true;

current_fs = $(this).parent();
previous_fs = $(this).parent().prev();

//de-activate current step on progressbar
$("#progressbar li").eq($("fieldset").index(current_fs)).removeClass("active");

//show the previous fieldset
previous_fs.show();
//hide the current fieldset with style
current_fs.animate({opacity: 0}, {
step: function(now, mx) {
//as the opacity of current_fs reduces to 0 - stored in "now"
//1. scale previous_fs from 80% to 100%
scale = 0.8 + (1 - now) * 0.2;
//2. take current_fs to the right(50%) - from 0%
left = ((1-now) * 50)+"%";
//3. increase opacity of previous_fs to 1 as it moves in
opacity = 1 - now;
current_fs.css({'left': left});
previous_fs.css({'transform': 'scale('+scale+')', 'opacity': opacity});
},
duration: 800,
complete: function(){
current_fs.hide();
animating = false;
},
//this comes from the custom easing plugin
easing: 'easeInOutBack'
});
});

$(".submit").click(function(){
return false;
})

这里是演示 - http://codepen.io/atakan/pen/gqbIz

最佳答案

您可以尝试我编写的这个 javascript 项目,可以在以下位置找到该项目:

https://www.npmjs.com/package/multi-step-form-js

https://github.com/mgildea/Multi-Step-Form-Js

您需要做的就是将每个表单步骤放入具有相应类的 div 中,如 README 中所述,并在表单对象上调用 javascript 函数:

$(".msf:first").multiStepForm();

这将使用 jquery unobtrusive 验证,或者您可以将验证对象作为参数传递以在不使用 unobtrusive 项目的情况下使用 jquery 验证。

在进入下一步之前,每个步骤都将通过 jquery 验证进行验证。

关于jquery - 多步骤表单验证jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28623672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com