2011-12-17 2 views
1

나는 모든 레일 형태와 같은 인 레일 형태가 무엇이든이 실패 할 경우 표준 레일 오류가 나타납니다rails errorExplanation이 내 jquery와 충돌합니까?

<div id="errorExplanation" class="errorExplanation"> 
    <h2>2 errors prohibited this account from being saved</h2> 
    <p>There were problems with the following fields:</p> 
    <ul><li>First Name can't be blank</li> 
    <li>Number is not a valid credit card number</li></ul> 
</div> 

하지만이 클릭 제출하면 스피너를 팝업 할 필요가 페이지에 몇 가지 jQuery를 가지고 :

$(document).ready(function(){ 
    $('#add_account').click(function(){ 
    console.log($('#errorExplanation').length); 
    $('#spinner').show(); 
    }); 
}); 

그러나 console.log은 항상 0이고 나는 페이지에는 #errorExplanation이없는 경우에만 표시 스피너가 필요합니다.

console.log에는 0이 있지만 하나가 있으므로 레일스가로드 된 후 페이지에서이 div를로드하고있는 것으로 추측합니다.

이 문제를 해결하고 페이지에 실제로 #errorExplanation이없는 경우에만 #spinner을 표시하는 방법은 무엇입니까?

+0

나는 click 이벤트가 _before_ rails 'form validator를 발생시키고 있다고 생각합니다. – Shad

답변

1
$(document).ready(function(){ 
    $('#add_account').click(function(){ 
    if(!$('#errorExplanation').length){ 
     $('#spinner').show(); 
    } 
    }); 
}); 

편집 : 사용 .length는 DIV의 내용을 계산합니다. 0 인 경우, 존재하지 않습니다.

편집 됨 :-)

+0

요소가있는 경우에만 true를 반환합니다. http://jsfiddle.net/W2nfx/. TS는 "페이지에 실제로 #errorExplanation이 없을 때"를 묻습니다. – Richard

+0

Eish, 내 잘못 :-(당신의 처벌을 받아들입니다 .http : //jsfiddle.net/W2nfx/1/ – Richard