2011-11-30 2 views
0
//Save new category information 
    $('.save_cat').live('click', function() { 

     cat_id = $(this).attr("id").slice(4); 
     cat_name = $(this).parent().prev().prev().children('.cat_name_edit').val(); 
     sort_order = $(this).parent().prev().children('.sort_order_edit').val(); 

     $.ajax({ type: 'POST', 
       url: '../file/category/update_category', 
       data: { 'cat_id' : cat_id, 
         'sort_order' : sort_order, 
         'cat_name' : cat_name}, 
       beforeSend:function(){ 

        //action while loading 

       }, 
       success:function(data){ 

        alert('hi'); 
        $(this).html("Edit"); 

       }, 
       error:function(){ 

        // failed request; give feedback to user 
        alert("An unexpected error has occurred. Your category could not be saved."); 
       }, 

       dataType : 'json' 
     }); 
    }); 

성공 핸들러에서 클릭 한 요소의 HTML을 저장에서 편집으로 변경하려고합니다. .save_cat의 클릭 핸들러 바로 아래에이 코드를 배치하면 올바르게 작동합니다. 요청이 성공하면 어떻게해야합니까?jquery ajax 성공 핸들러에서 액세스하기

답변

6

live 처리기 로컬 변수에서 해당 값을 캡처해야합니다. 1.7+] 2을 jQuery를

$('.save_cat').live('click', function() { 
    var $that = $(this); 

    // These should be vars. 
    var cat_id = $that.attr("id").slice(4); 
    ... 
    $.ajax({ ... 
     success: function() { $that.html("Edit"); } 

FWIW JQuery와 < 1.7 이상 .live.delegate is preferred.on is preferred.

+0

감사합니다, 이것은 내가 찾고있는 것입니다 : – ThinkingInBits

+0

라이브 핸들러의 첫 줄은'var element = $ (this); '이어야하고'success' 핸들러에서'element' 변수를 조작해야합니다. – MilkyWayJoe

관련 문제