如何在 asp.net 核心中使用模型驗證提交表單后禁用按鈕?
代碼:
<form action="/AddComment" method="post" id="InsertCommentForm">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div >
<input type="hidden" asp-for="ProductId" />
<input type="hidden" asp-for="ParentId" value="" id="reply" />
<input asp-for="Email" id="Email" placeholder="????? ?? ????? ????? ???? ????">
<span asp-validation-for="Email" ></span>
<input asp-for="FullName" id="FullName" placeholder="????? ??? ??? ?? ???? ????">
<span asp-validation-for="FullName" ></span>
<textarea asp-for="Text" id="Text" cols="60" rows="5" placeholder="???? ??? ???? ?? ???? ????"></textarea>
<span asp-validation-for="Text" ></span>
<br />
<input type="submit" value="?????" id="InsertCommentBtn" >
</div>
</form>
我測驗了幾次,但如果驗證模型開始作業并阻止表單發布,則禁用按鈕仍然存在。
謝謝
uj5u.com熱心網友回復:
您需要使用validate()方法jquery.validate.js(默認在_ValidationScriptsPartial.cshtml中參考了js檔案)來驗證表單。只有當驗證通過時,按鈕才會被禁用:
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
<script>
$("#InsertCommentForm").on("submit", function(event) {
var validator = $("#InsertCommentForm" ).validate();
var flag = validator.form();
if(flag)
{
$('input[type="submit"]').attr('disabled',true);
}
});
</script>
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/425405.html
標籤:javascript jQuery asp.net 核心 验证
