2011-09-19 4 views
2

이들의 차이점은 무엇이며 언제 어떤 것을 사용해야하는지 어떻게 알 수 있습니까?이러한 POST 명령의 차이점은 무엇입니까?

$.post($(this).attr('action'), $(this).serialize(), function(response) { 
    // do something here on success 
}, 'json'); 

$.post($(this).prop('action'), $(this).serialize(), function(response) { 
    // do something here on success 
}, 'json'); 

$.post($(this).closest("form").prop('action'), $(this).serialize(), function(response) { 
    // do something here on success 
}, 'json'); 

답변

2

이 경우 첫 번째와 두 번째는 동일합니다. 이러한 함수는 양식 이벤트 처리기에서 호출해야합니다. $(this).prop/attr() 대신에 $(this)[0].actionthis.action도 사용할 수 있습니다.

세 번째 방법은 가장 가까운 양식 요소를 찾고 양식의 action 특성을 검색합니다. 이 메소드는 비 형식 컨텍스트 (non-form context)에서 유용합니다. button 요소에서

1

첫 번째와 두 번째는 <form> 요소와 연결된 "submit"이벤트 처리기에 적합합니다. 두 번째는 아마도 더 좋을지 모르지만 실제 사용에서는 거의 동일합니다.

세 번째는 <form> 또는 유사한 상황에서 <button>의 "클릭"처리기로 유용합니다.

관련 문제