[英]validation false but the form is still submitted
I want to validate whether the number inputted by user exist in the database so I have a action to return a JSON object and check the input before post to server. 我想验证数据库中是否存在用户输入的数字,因此我有一个操作要返回JSON对象并在输入到服务器之前检查输入。 The problem is it always return false. 问题在于它总是返回false。
Javascript: 使用Javascript:
<script type="text/javascript">
var pid = $("#ProductID").val();
var flag = false;
$("#button1").click(function () {
$.get('/Invoice/GetData',
function (data) {
for (var i = 0; i < data.length; i++) {
if (data[i] == pid) {
flag = true;
}
}
});
if (flag == false) {
alert("Invalid Product ID");
location.reload();
return false;
}
else {
alert("validations passed");
return true;
}
});
</script>
Form (ASP.NET MVC4) 表格(ASP.NET MVC4)
@using (Html.BeginForm("AddInvoiceDetails","Invoice",FormMethod.Post)) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Invoice_Detail</legend>
<div class="editor-label">
@Html.LabelFor(model => model.InvoiceID)
</div>
<div class="editor-field">
@Html.TextBox("InvoiceID", (String)ViewBag.invoiceid,new { @readonly="readonly" })
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ProductID)
</div>
<div class="editor-field">
@Html.EditorFor(model=>model.ProductID)
@Html.ValidationMessageFor(model => model.ProductID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Quantity)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Quantity)
@Html.ValidationMessageFor(model => model.Quantity)
</div>
<p>
<input id="button1" type="submit" value="Create" />
</p>
</fieldset>
}
You are using ajax which is asynchronous, so your if statements will be executed before $.get()
is done executing. 您正在使用异步的ajax,因此您的if语句将在$.get()
完成执行之前执行。 You could do this: 您可以这样做:
$("#button1").click(function (event) {
$.get('/Invoice/GetData',
function (data) {
for (var i = 0; i < data.length; i++) {
if (data[i] == pid) {
flag = true;
}
}
if (flag == false) {
alert("Invalid Product ID");
location.reload();
event.preventDefault(); //prevents post to server
}
else {
alert("validations passed");
}
});
});
Find the solution with the help of a friend, and all other answers really help. 在朋友的帮助下找到解决方案,所有其他答案确实有帮助。 The solution is to change the type of "button1"
into button and have it submit the form if validation is true and display a dialog if it is false. 解决方案是将"button1"
的类型更改为button,如果验证为true,则提交表单,如果为false,则显示对话框。 The code: 编码:
Javascript: 使用Javascript:
<script type="text/javascript">
$("#button1").live("click",function (event) {
$.get('/Invoice/GetData',
function (data) {
var pid = $("#ProductID").val();
var flag = false;
for (var i = 0; i < data.length; i++) {
if (data[i] == pid) {
flag = true;
}
}
if (flag == false) {
alert("Invalid Product ID");
return false;
}
else {
$("#myform").submit();
return true;
}
});
});
Form (ASP.NET MVC4) 表格(ASP.NET MVC4)
@using (Html.BeginForm("AddInvoiceDetails", "Invoice", FormMethod.Post, new { id = "myform", data_ajax = "false" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Invoice_Detail</legend>
<div class="editor-label">
@Html.LabelFor(model => model.InvoiceID)
</div>
<div class="editor-field">
@Html.TextBox("InvoiceID", (String)ViewBag.invoiceid, new { @readonly = "readonly" })
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ProductID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ProductID)
@Html.ValidationMessageFor(model => model.ProductID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Quantity)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Quantity)
@Html.ValidationMessageFor(model => model.Quantity)
</div>
<p>
<input id="button1" type="button" value="Create" />
</p>
</fieldset>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.