2010-06-29 59 views
0

我有一个表单,我正在使用jQuery和php进行验证。所以基本上,如果PHP的回声“输入必须填充”,jQuery应该在该输入周围放置一个红色边框,但是只有在提交表单两次后才能使用。
我解释:如果我提交输入填充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

它看起来像通过表单提交表单而不是你的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; 
}) 
+0

刚刚尝试过,但仍然无法正常工作。 它总是工作,但在第二次按下提交.. – tetris 2010-06-29 14:33:10

+0

好吧,我改变了如果条件成功事件,现在没事的。 – tetris 2010-06-29 14:37:26