2012-07-31 48 views
0

我使用bitrepository.com伟大AJAX形式,它利用jQuery和PHP:http://www.bitrepository.com/a-simple-ajax-contact-form-with-php-validation.htmljQuery的淡入新DIV

我想点击后出现掉色的错误和验证DIV

增加了以下的jQuery代码,但无济于事(也试图与点击/更改功能):

$(document).ready(function(){ 
    if ($('.notification_error').length > 0) { 
     $('.notification_error').fadeTo("slow", 1.0); 
    } 
}); 

任何帮助将是巨大的......

+2

只是FYI,你不需要if语句。如果你的选择器不匹配任何元素,那么没有元素会褪色并且不会抛出任何错误,所以你可以摆脱if语句。 – 2012-07-31 18:26:02

+2

这似乎只在文档加载时运行一次。你想在用户点击一个提交按钮后执行fadeTo,对吧?当用户点击按钮时执行的代码是什么? – AceCorban 2012-07-31 18:26:35

+1

在这种情况下使用if语句是不必要的。如果.notification_error不存在,JQuery将不会抛出任何异常。 – 2012-07-31 18:29:13

回答

2

鉴于你的代码看起来完全像你提到的一个,你可以在你的AJAX完成回调改成这样:

$("#note").ajaxComplete(function(event, request, settings){ 

    if(msg == 'OK') // Message Sent? Show the 'Thank You' message and hide the form 
    { 
     result = '<div class="notification_ok">Your message has been sent. Thank you!</div>'; 
     $("#fields").hide(); 
    } 
    else 
    { 
     result = msg; 
    } 
    /* Hide the element, alter the content, and then fade it in */ 
    $(this).hide().html(result).fadeIn("slow"); 
}); 

这两个无差错和成功褪色-messages。

更新:

要仅淡出在第一时间的元素,你可以像这样的东西代替的最后一行:

if ($(".notification_error, .notification_ok", this).length === 0) { 
    $(this).hide().html(result).fadeIn("slow"); 
} else { 
    $(this).html(result); 
} 

,我们要做的是,我们检查是否有一个en类元素,具有notification_errornotification_ok。如果没有(第一次)我们淡入,否则我们只是更新内容。

+0

完美 - 谢谢! – Alex 2012-07-31 18:49:21

+1

@Alex我的荣幸! – 2012-07-31 18:50:23

+0

如果您有时间,快速跟进;因为我们添加了.hide(),如果您单击提交多次并显示错误,则DIV会隐藏并淡入。如果存在HTML,是否有办法阻止DIV隐藏()? – Alex 2012-07-31 19:01:28

1

次尝试E采用:

$("#clicked_input_id").click(function(){ 
    $('.notification_error').fadeTo("slow", 1.0); 
});