html中form异步验证,验证FORM中的DIV(可见向导步骤)内的HTML输入元素?

本文探讨了如何在HTML5中实现一个向导式的窗体,并通过ASP.NET MVC进行表单验证。重点讨论了如何仅验证当前显示的步骤中的输入字段,而非整个表单。

我决定在HTML 5中创建一个向导窗体(实际上在这里使用ASP.NET MVC)。我有以下的HTML表单:验证FORM中的DIV(可见向导步骤)内的HTML输入元素?

@using (Html.BeginForm())

{

Back

Next

}

然后,我有这个js脚本:

$(document).ready(function() {

var $steps = $(".wizard-step");

var index = 0;

var count = $steps.length;

$steps.each(function() {

$(this).hide();

});

$(".back-button").attr("disabled", "disabled");

var $currentStep = $steps.first();

$currentStep.show();

$currentStep.addClass("current-step");

$(".back-button").click(function() {

$currentStep.hide();

$currentStep.removeClass("current-step");

$currentStep = $currentStep.prev();

$currentStep.addClass("current-step");

$currentStep.show();

index--;

$(".next-button").removeAttr("disabled");

if (index == 0) {

$(this).attr("disabled", "disabled");

}

else {

$(this).removeAttr("disabled");

}

});

$(".next-button").click(function() {

var $inputFields = $(".current-step :input");

var hasError = false;

$inputFields.each(function() {

if (!validator.element(this)) {

hasError = true;

}

});

if (hasError)

return false;

index++;

$(".back-button").removeAttr("disabled");

if (index == count - 1) {

$(this).attr("disabled", "disabled");

}

else {

$(this).removeAttr("disabled");

}

$currentStep.hide();

$currentStep.removeClass("current-step");

$currentStep = $currentStep.next();

$currentStep.addClass("current-step");

$currentStep.show();

});

});

基本上,我想是在点击下一步按钮,它会验证当前发现里面输入元素可见的DIV,而不是整个表格。是否有可能使用HTML5做到这一点?如果没有,也许jQuery?

如果您有其他人,请在此分享。非常感谢!

+0

我不得不说,在对代码进行快速浏览之后,似乎单击Next按钮时,它只会验证“.current-step”div内的输入,而不是整个表单。这不是你想要的吗? –

+0

是的,那是我需要的。当前脚本没有按预期工作。 –

+0

我想这很大程度上取决于'validator.element(this)'的作用......你有这个代码吗? –

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值