我有一个带有简单表单的ASP.Net Core/Razor应用程序:
<form method="post" id="my_form">
...
</form>我正在使用内置的客户端验证,比如<input ...required>或<input type="number" min="0" ...> (微软在他们的MSVS项目模板中包括了jQuery验证)。一切都很好。
我还在对某些字段进行一些额外的自定义验证。我想给用户一个选项,要么重新做表单,要么退出表单,然后重定向到另一个页面:
<script type="text/javascript">
// Form submit
$('#my_form').on('submit', function (e) {
console.log('validateForm...');
//debugger;
// Check basic form validation
let isValid = e.target.checkValidity();
if (!isValid)
return false;
// Validate custom field
let myCustomField = $('#myCustomField').val();
if (myCustomField != 'xyz') {
let response = confirm('MyCustom field is invalid: Update webform?');
if (response) {
return false; // Return to webform
} else {
window.location.href = "./"; // Redirect to app's landing page
}
...
return true;
}
});
</script>问题:
window.location.href不会立刻重新引导我。验证JS继续进行,表单是POSTed到服务器。
问:我应该用像location.replace()这样的东西吗?还是我应该采取完全不同的方法?
发布于 2022-04-01 20:04:27
回答瑞凯·冯:不,阿贾克斯会挫败我想做的事情的全部目的。我想让用户可以选择:( a)返回到web表单以“更正”它,或者( b)完全离开表单,跳转到另一个页面。Ajax不会这么做的。
我决定“踢”,让我的JS验证函数返回“假”,如果它发现任何错误。这总是将用户返回到web表单。他们可以“修复”问题,也可以手动浏览到不同的页面。
https://stackoverflow.com/questions/71684848
复制相似问题