【问题标题】:Can you use $(this) on an jquery ajax finish function?你可以在 jquery ajax 完成函数上使用 $(this) 吗?
【发布时间】:2012-05-20 21:48:48
【问题描述】:

现在,当我从我的 ajax 发布请求中取回数据时,我正在尝试使用 $(this).insertFUnctionNameHere()。我试着做一些测试,比如 $(this).parent().hide(); $(this).slideUp();看看它在哪里选择,但它似乎什么也没选择?这是正常的还是我有办法让 $(this) 实际选择一些东西。

这是我的代码。

/*
         * This is the ajax
         */
        $(".reply").on( "submit" , function(){
        // Intercept the form submission
        var formdata = $(this).serialize(); // Serialize all form data
        // Post data to your PHP processing script
        $.post( "/comment.php", formdata, function( data ) {
            // Act upon the data returned, setting it to .success <div>
            $(this).children().slideUp();
            $(this).children('.success').prepend(data);
            $('.hidden').hide();
        });
        return false;
        });
        /*
         * End ajax
         */

我使用它的网站链接是HERE (readysetfail.com)

【问题讨论】:

    标签: jquery ajax post request return


    【解决方案1】:

    试试这个:

    $(".reply").on( "submit" , function(){
        var reply = $(this);
        ...
        $.post( "/comment.php", formdata, function( data ) {
            reply.children().slideUp();
            reply.children('.success').prepend(data);
            ...
        });
        ...
    

    【讨论】:

    • 天哪,它成功了!!!!啊啊啊非常感谢!几天来我一直在努力解决这个问题!非常感谢!
    【解决方案2】:

    我不知道你是否决定使用 $(this),但如果没有,我会将引用表单的 jQuery 对象(通过在事件处理程序中使用 $(this) 检索)保存在一个变量中,然后在完成函数中使用它,如下所示:

    $(".reply").on( "submit" , function(){
    // Intercept the form submission
    var formdata = $(this).serialize(); // Serialize all form data
    var theForm = $(this);
    // Post data to your PHP processing script
    $.post( "/comment.php", formdata, function( data ) {
        // Act upon the data returned, setting it to .success <div>
        theForm.children().slideUp();
        theForm.children('.success').prepend(data);
        $('.hidden').hide();
    });
    return false;
    });
    

    【讨论】:

    • 这也有效!与第一个响应的想法相同,但它有效!惊人的!我很感激!
    猜你喜欢
    • 2011-06-26
    • 1970-01-01
    • 2015-05-01
    • 1970-01-01
    • 2014-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    相关资源
    最近更新 更多