我有一个表单,我正在使用jQuery和php进行验证。所以基本上如果php回应“输入必须被填充”,jQuery应该在该输入周围放置一个红色边框,但只有在提交表单两次后才能工作。 我解释一下:如果我提交输入未填充的php文件回应“输入必须填写”,但只有当我再次按下提交按钮时 - 输入变为红色。
$("form#maj_email").submit(function(){
var _data = $(this).serialize()
$.ajax({
type: 'POST',
url: 'validation_profil.php?var=maj_email',
beforeSend: function(){
$("div#ajax_icon_maj_email").css({background:"url('http://localhost/www3/images/ajax_loader.gif')"})
$("div#error_maj_email").hide()
if( $("div#error_maj_email").text()=="Email syntaxe incorrecte"){
$("form#maj_email input:[name=email]").css({border:"1px solid red"})
}
},
data:_data,
cache: false,
success: function(html){
$('div#error_maj_email').html(html)
$("div#ajax_icon_maj_email").css({background:"url('none')"})
$("div#error_maj_email").fadeIn()
}
})
})
答案 0 :(得分:0)
看起来表单是通过表单而不是您的ajax调用提交的。您需要阻止此行为才能实现此目的:
$("form#maj_email").submit(function(e){
var _data= $(this).serialize();
$.ajax({
type: 'POST',
url: 'validation_profil.php?var=maj_email',
beforeSend: function(){
$("div#ajax_icon_maj_email").css({background:"url('http://localhost/www3/images/ajax_loader.gif')"})
$("div#error_maj_email").hide()
if( $("div#error_maj_email").text()=="Email syntaxe incorrecte"){
$("form#maj_email input:[name=email]").css({border:"1px solid red"})
}
},
data:_data,
cache: false,
success: function(html){
$('div#error_maj_email').html(html)
$("div#ajax_icon_maj_email").css({background:"url('none')"})
$("div#error_maj_email").fadeIn()
}
});
e.preventDefault();
return false;
})