我有一个弹出表单,它有一个文本框,我通过jQuery使用AJAX帖子将表单提交到服务器。我使用强类型的部分视图作为弹出窗口,但当我保持文本框为空并单击提交按钮时,它会显示一个成功的验证并将表单发布到服务器。
这是我的ajax代码:
$(document).ready(function(){
//if($("#frmScenaria").valid()==true)
//code to save detail
$('#frmPost').submit(function (e) {
var serializedForm = $('#frmPost').serialize();
$.ajax({
url: '@Url.Action("Save","Scenario")',
type: "POST",
dataType: "json",
data: serializedForm,
success: function (result) {
if (result.Success==true)
{
location.reload();
}
},
error: function(xhr, status, error) {
//alert(error.Message);
}
});
return false;
});
//code to save detail end
});
有人能帮我解决这个问题吗?
请改用客户端验证。如下所示:[假设textbox1
是您的文本框的名称]
$('#frmPost').submit(function (e) {
if($('#frmPost').textbox1.value == "") {
alert('Please enter a value');
$('#frmPost').textbox1.focus();
return false;
}
var serializedForm = $('#frmPost').serialize();
$.ajax({
url: '@Url.Action("Save","Scenario")',
type: "POST",
dataType: "json",
data: serializedForm,
success: function (result) {
if (result.Success==true)
{
location.reload();
}
},
error: function(xhr, status, error) {
//alert(error.Message);
}
});
return false;
});
希望这对你有用,谢谢。
请使用以下内容注意:在这里,我在发布之前检查了来自vqaValidation的。
$('#frmPost').click(function (e) {
if($("#frmPost").validate())
{
var serializedForm = $('#frmPost').serialize();
$.ajax({
url: '@Url.Action("Save","Scenario")',
type: "POST",
dataType: "json",
data: serializedForm,
success: function (result) {
if (result.Success==true)
{
location.reload();
}
},
error: function(xhr, status, error) {
//alert(error.Message);
}
});
return false;
}
});