2016-04-25 61 views
0

我有下面的jQuery代碼來檢查開始日期和結束日期,但錯誤消息僅顯示一次,如果用戶更改了任何錯誤值的結束日期,則不會生成錯誤消息。有什麼我失蹤?只生成一次錯誤消息

$('input[name="endDate"]').change(function(){ 
    var startDate=$('input[name="startDate"]'); 
    var endDate=$('input[name="endDate"]'); 
    var errorSpan= $('.errorSpan'); 
    $.ajax({ 
     url:'${createLink(controller:'empRef', action: 'checkServiceDatesAjax')}' , 
     type:'POST' , 
     data:{startDate:startDate.val(), endDate:endDate.val()} , 
     success: function(XMLHttpRequest, textStatus, jqXHR) { 

     }, 

     error: function(XMLHttpRequest, textStatus, errorThrown) { 
      errorSpan.html("&nbsp;&nbsp;&nbsp;&nbsp;<font color='red'>"+XMLHttpRequest.responseText+"</font>").fadeOut(5000); 
      endDate.focus(); 

     } 
    }) 

}); 

回答

1

這是因爲當你的錯誤消息容器上稱之爲​​,它會將其display屬性none。當您嘗試重複使用相同的元素時,它已被隱藏。您可以通過確保元素在淡出之前可見來使其工作:

error: function(XMLHttpRequest, textStatus, errorThrown) { 
    errorSpan.html("&nbsp;&nbsp;&nbsp;&nbsp;<font color='red'>"+XMLHttpRequest.responseText+"</font>") 
    errorSpan.show() 
    errorSpan.fadeOut(5000); 
    endDate.focus(); 
}