我决定在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)'的作用......你有这个代码吗? –
本文探讨了如何在HTML5中实现一个向导式的窗体,并通过ASP.NET MVC进行表单验证。重点讨论了如何仅验证当前显示的步骤中的输入字段,而非整个表单。
222

被折叠的 条评论
为什么被折叠?



