我正在使用此代码:
function submitForm() {
$.ajax({type:'POST',
url: 'index.php',
data:$('#ContactForm').serialize(),
success: function(response) {
$('#ContactForm').find('.form_result').html(response);
}}
);
return false;
}
它运行良好,但我如何添加验证,例如......以在用户名或密码为空时发出警报?
我知道周围有很多类似的脚本,但我想特别将其添加到这个脚本中。
最佳答案
使用beforeSend()
function submitForm() {
$.ajax({type:'POST',
url: 'index.php',
data:$('#ContactForm').serialize(),
success: function(response) {
$('#ContactForm').find('.form_result').html(response);
}},
beforeSend: function(){
if(!$("#name").val()){
alert("name field is empty.");
return false;
}
}
);
return false;
}
关于javascript - JQuery Ajax提交功能添加验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22113248/