javascript - 除非向导的每个选项卡都经过验证,否则阻止向导下一步按钮继续
问题描述
我的 MVC5 项目中有一个向导,它使用 MVC 的内置验证。我想阻止向导继续进行,除非当前选项卡中的所有字段都通过验证。据我所知,MVC 验证仅在用户单击提交时进行验证。除非所有字段都有效,否则当用户单击下一步按钮时,如何防止用户进入向导中的下一个选项卡?我试图用 Javascript 来做,但没有用。我相信它只是进行客户端验证(与 mvc 相同)有没有办法在 C# MVC 中做到这一点?如果没有,有人可以解释为什么我的 javascript 无法验证吗?allNextBtn.click(function ()) 应该解决这个问题。当用户单击下一个按钮时,此功能应触发并阻止用户前进,但事实并非如此。
控制器:
[HttpPost]
public ActionResult Create(LoginSecurityQuestionsViewModel chargeDto)
{
if (ModelState.IsValid == false)
{
LoginSecurityQuestionsViewModel viewModel = new LoginSecurityQuestionsViewModel();
viewModel.chlist1 = new SelectList((from s in db.churches
select new
{
ID = s.id,
FullName = s.ChurchName + " " + s.ChuchDenomination + " " + s.ChurchType + " - " + s.ChurchCity + ", " + s.ChurchState
}),
"ID",
"FullName");
viewModel.chlist2 = new SelectList((from s in db.churches
select new
{
ID = s.id,
FullName = s.ChurchName + " " + s.ChuchDenomination + " " + s.ChurchType + " - " + s.ChurchCity + ", " + s.ChurchState
}),
"ID",
"FullName");
viewModel.chlist3 = new SelectList((from s in db.churches
select new
{
ID = s.id,
FullName = s.ChurchName + " " + s.ChuchDenomination + " " + s.ChurchType + " - " + s.ChurchCity + ", " + s.ChurchState
}),
"ID",
"FullName");
viewModel.chlist4 = new SelectList((from s in db.churches
select new
{
ID = s.id,
FullName = s.ChurchName + " " + s.ChuchDenomination + " " + s.ChurchType + " - " + s.ChurchCity + ", " + s.ChurchState
}),
"ID",
"FullName");
viewModel.seclist = new SelectList(db.secretquestions, "id", "Question");
viewModel.subscriptionTypes = new SelectList((from s in db.subscriptiontypes
select new
{
ID = s.StripePriceID,
FullName = s.SubscriptionType1 + " - $" + s.Price + "/Month "
}),
"ID",
"FullName");
viewModel.musicianTypes1 = new SelectList(db.musiciantypes, "id", "TypeName");
viewModel.musicianTypes2 = new SelectList(db.musiciantypes, "id", "TypeName");
return View(viewModel);
}
向导的 Javascript:
<script type="text/javascript">
$(document).ready(function () {
var navListItems = $('div.setup-panel div a'),
allWells = $('.setup-content'),
allNextBtn = $('.nextBtn');
allWells.hide();
navListItems.click(function (e) {
e.preventDefault();
var $target = $($(this).attr('href')),
$item = $(this);
if (!$item.hasClass('disabled')) {
navListItems.removeClass('btn-success').addClass('btn-default');
$item.addClass('btn-success');
allWells.hide();
$target.show();
$target.find('input:eq(0)').focus();
}
});
allNextBtn.click(function () {
var curStep = $(this).closest(".setup-content"),
curStepBtn = curStep.attr("id"),
nextStepWizard = $('div.setup-panel div a[href="#' + curStepBtn + '"]').parent().next().children("a"),
curInputs = curStep.find("input, select"),
isValid = true;
$(".form-group").removeClass("has-error");
for (var i = 0; i < curInputs.length; i++) {
if (!curInputs[i].validity.valid) {
isValid = false;
$(curInputs[i]).closest(".form-group").addClass("has-error");
}
}
if (isValid) nextStepWizard.removeAttr('disabled').trigger('click');
});
$('div.setup-panel div a.btn-success').trigger('click');
});
</script>
<script>
$('.select2').select2();
</script>
解决方案
推荐阅读
- java - 如何在 Thymeleaf 中动态替换片段?
- c# - 使用 autofac 时,有没有办法将现有视图模型作为数据上下文传递给最近打开的对话框窗口?
- python - 如何在 pymongo 中正确设计正则表达式?
- r - 如何在选择中的 dplyr 函数中传递列参数?
- jsf - 将自定义处理程序类添加到 JSF 自定义组件
- botframework - WebChat 程序化发布活动不起作用 - 单击按钮时未发送消息
- datetime - 如果组的最后一个值丢失,则替换为 POSIXct 变量
- php - 如何在 MacOS 上通过 asdf-php 插件安装 PHP 7.2.18
- django - django @login_required 可以在移动应用程序使用的 API 上使用吗?
- python - Airflow 中 DAG 的 post_execution 挂钩