2011-12-22 11 views
1

私はAJAXリクエストからいくつかのhtmlを取得し、テーブルの1行を隠して別のものを表示するコードを持っていますが、htmlを保持するために作成するvar(quick_edit_html )は、AJAX関数が実行された後(アラートボックスに入れてテストされます)、AJAXリクエストが完了するまで実行されていない次の関数で使用しようとすると、 。AJAX/JQuery - varは存在しません

私が間違っているつもりだどこ上の任意のアイデア?

/** Run the AJAX request to grab the qucik edit html */ 
var load_quick_edit = jQuery.post(MyAjax.ajaxurl, data, function(response){ 
    var quick_edit_html = response; 
}); 

/** Display the correct quick edit row */ 
load_quick_edit.done(function(){ 

    /** Hide the row that is to be edited */ 
    jQuery('tr#display-'+slug).hide(); 

    /** Show the quick edit row that the user has requested */ 
    jQuery('tr#quick-edit-'+slug).show(); 
    jQuery('tr#quick-edit-'+slug).html(quick_edit_html); 

}); 

ありがとう。

答えて

2

あなたのVARは、匿名のAJAXコールバック関数内で宣言されています。これはvarをその関数にスコープし、他の場所からはアクセスできないことを意味します。

は、単にあなたの関数の外VARを宣言します。

var quick_edit_html; 

/** Run the AJAX request to grab the qucik edit html */ 
var load_quick_edit = jQuery.post(MyAjax.ajaxurl, data, function(response){ 
    quick_edit_html = response; 
}); 

/** Display the correct quick edit row */ 
load_quick_edit.done(function(){ 

    /** Hide the row that is to be edited */ 
    jQuery('tr#display-'+slug).hide(); 

    /** Show the quick edit row that the user has requested */ 
    jQuery('tr#quick-edit-'+slug).show(); 
    jQuery('tr#quick-edit-'+slug).html(quick_edit_html); 

}); 
+0

おかげ - それは愚かなものになるだろうと思いました! –

1

quick_edit_htmlは、その時点でスコープの外になっている必要があります..あなたは(このキーワードで)グローバルスコープやプロトタイプのスコープに格納するのいずれか試すことができます

+0

私は 'this'キーワードを使用する実装を見ることに興味があります。私は常にグローバルまたは名前空間のスコープに頼っていますが、代わりの方法は持っているといいです。 –

+0

ありがとう、それはそれをソートしました。 –

2

それは外のだからそれは利用できません範囲。 post()外に宣言します。

var quick_edit_html; 

    /** Run the AJAX request to grab the qucik edit html */ 
var load_quick_edit = jQuery.post(MyAjax.ajaxurl, data, function(response){ 
    quick_edit_html = response; 
}); 

    /** Display the correct quick edit row */ 
load_quick_edit.done(function(){ 
    /** Hide the row that is to be edited */ 
    jQuery('tr#display-'+slug).hide(); 

    /** Show the quick edit row that the user has requested */ 
    jQuery('tr#quick-edit-'+slug).show(); 
    jQuery('tr#quick-edit-'+slug).html(quick_edit_html); 
}); 
+0

ありがとう、それはトリックです。 –

関連する問題