AJAX/JQuery-var不存在

AJAX/JQuery-var不存在,jquery,ajax,var,Jquery,Ajax,Var,下面的代码从AJAX请求中获取一些html,然后隐藏表中的一行并显示另一行,但即使我创建用于保存html的var(quick\u edit\u html)在AJAX函数运行后可用(通过将其放在警报框中进行测试),Firebug告诉我,当我尝试在下一个函数中使用它时,它并不存在(在AJAX请求完成之前,它不会运行) 你知道我哪里出错了吗 /** Run the AJAX request to grab the qucik edit html */ var load_quick_edit = jQu

下面的代码从AJAX请求中获取一些html,然后隐藏表中的一行并显示另一行,但即使我创建用于保存html的
var
(quick\u edit\u html)在AJAX函数运行后可用(通过将其放在警报框中进行测试),Firebug告诉我,当我尝试在下一个函数中使用它时,它并不存在(在AJAX请求完成之前,它不会运行)

你知道我哪里出错了吗

/** 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);

});

谢谢。

您的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);

});

您的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);

});

quick_edit_html此时应该已超出范围。。您可以尝试将其存储在全局范围或原型范围内(使用此关键字)

quick\u edit\u html此时应该已超出范围。。您可以尝试将其存储在全局范围或原型范围(使用此关键字)

它不可用,因为它超出了范围。在
post()之外声明它。


它不可用,因为它超出范围。在
post()之外声明它。


我很想看到一个使用“this”关键字的实现。我总是依赖于全局或名称空间的作用域,但是有替代方法是很好的。我很想看到一个使用“this”关键字的实现。我总是依赖于全局或命名空间的作用域,但是有其他方法是很好的。