2013-05-14 24 views
1

我有這樣的jQuery代碼和我jQuery的操縱被傳遞的對象引用

$('.parent').livequery('change', function() { 

     $(this).parent('.show_sub_categories').append('<img src="nlevel_ajax_dropdown/loader.gif" style="float:left; margin-top:7px;" id="loader" alt="" />'); 

     $.post("nlevel_chid_categories.php", { 
      parent_id: $(this).val(), 
     }, function(response){ 
      var ref = $(this).parent('.show_sub_categories'); 
      setTimeout("finishAjax('show_sub_categories', '"+escape(response)+"','"+ref+"')", 400); 
     }); 

     return false; 
    }); 

}); 

function finishAjax(id, response,ref){ 
    $('#loader').remove(); 
    $(ref).append(unescape(response)); // and this is giving error 
} 

元素我想提出的是阿賈克斯完成後調用finishAjax功能,並將結果追加到元素的父元素上事件所以我試圖通過var ref = $(this).parent('.show_sub_categories');獲得其父母的參考,並通過finishAjax調用setTimeout函數,但這是拋出jquery錯誤。

下面是實際工作中的錯誤信息: Syntax error, unrecognized expression: [object Object]
(function(e,t){function _(e){var t=M[e...y",[],function(){return v})})(window);

+0

應該添加錯誤。 – MisterJ 2013-05-14 07:30:20

+0

@MisterJ檢查更新後的帖子。 – ravisoni 2013-05-14 07:35:38

回答

1

你可以用.done()做到這一點,而不是增加一個額外的參數post。另外,匿名函數更容易調試,特別是當您需要在setTimeout語法中傳遞參數時。試試這個:

$('.parent').livequery('change', function() { 
    var ref=$(this).parent('.show_sub_categories'); 

    ref.append('<img src="nlevel_ajax_dropdown/loader.gif" style="float:left; margin-top:7px;" id="loader" alt="" />'); 

    var ajaxPost = $.post("nlevel_chid_categories.php", { 
     parent_id: $(this).val() 
    }); 

    ajaxPost.done(function(response){ 
     setTimeout(function(){ 
      finishAjax('show_sub_categories', response, ref); 
     }, 400); 
    }); 

    return false; 
}); 

編輯:調整ref定義中使用它遍佈+傳遞的response的轉義版本在.done部分。

同樣,在finishAjax通過ref已經是一個選擇,所以應該直接在此時訪問:

function finishAjax(id, response, ref){ 
    $('#loader').remove(); 
    ref.append(response); 
} 
+0

@ frederik -l nothing happend ..:'( – ravisoni 2013-05-14 07:55:04

+0

其實這條線沒有得到$(this)的父項var ref = $(this).parent('。show_sub_categories');我已經通過將它放在外面$ .post調用有ahs parent。 – ravisoni 2013-05-14 07:56:20

+0

我更新了我的答案,以使它使用相同的選擇器。至於另一個錯誤,您需要確保附加一個字符串而不是一個對象。什麼是'nlevel_chid_categories。 php' return?是json還是純文本? – 2013-05-14 08:01:50