jQuery表单随验证一起提交
当创建jQuery函数来提交表单时,我需要在发送请求之前首先进行验证。这是我的密码:jQuery表单随验证一起提交,jquery,forms,Jquery,Forms,当创建jQuery函数来提交表单时,我需要在发送请求之前首先进行验证。这是我的密码: $('#Form').submit(function(e){ e.preventDefault(); e.stopImmediatePropagation(); var PhotoID = $('#PhotoID').val(); // I will set the PhotoID to 0 just for testing. var PhotoID = '0';
$('#Form').submit(function(e){
e.preventDefault();
e.stopImmediatePropagation();
var PhotoID = $('#PhotoID').val();
// I will set the PhotoID to 0 just for testing.
var PhotoID = '0';
if (PhotoID == '0') {
$('#GlobalError').removeClass('hidden');
} else {
return true;
$.ajax({
type: "POST",
url: $(this).attr( 'action' ),
data: $(this).serialize(),
beforeSend: function(){
$("#loader").fadeIn(2000);
},
success: function( response ) {
}
});
}
});
当
PhotoID
与0
不同时,我想提交表单,并按照常规页面更改表单操作属性。我知道e.preventDefault()
的反面是返回true代码>但不起作用。该功能正在正确触发。因此,问题在于表单提交处理。使用返回false来避免表单提交
$('#Form').submit(function(e){
var PhotoID = $('#PhotoID').val();
// I will set the PhotoID to 0 just for testing.
var PhotoID = '0';
if (PhotoID == '0') {
$('#GlobalError').removeClass('hidden');
return false;
} else {
$.ajax({
type: "POST",
url: $(this).attr( 'action' ),
data: $(this).serialize(),
beforeSend: function(){
$("#loader").fadeIn(2000);
},
success: function( response ) {
}
});
}
});
但它从未提交,因为您正在将PhotoID
值设置为0它工作了。我标记它只是为了更好地理解代码。我将在将来为有相同问题的人更正代码。非常感谢。