Home > Web Front-end > JS Tutorial > body text

Problem with calling $(this) in jquery ajax callback function

伊谢尔伦
Release: 2016-11-22 11:34:50
Original
1035 people have browsed it

A problem occurred when using jquery's ajax callback function, that is, $(this) cannot be used in the callback function. I have been working on this problem for a long time, but I can't solve it. Later, I saved $(this) into a variable before calling the function, and then used it in the callback function. There was no problem. I will make a note here.

$(document).ready(function(){
    $(“.del”).click(function () {
        obj=$(this).parent();//回调函数前先写入变量;
        delid=$(this).attr(‘delid’);
        $.get(‘drag.php’,{‘op’ : ‘del’,'id’ : delid},function(data){
            if(data==1){
                alert(‘删除成功’);
                obj.remove();//直接写$(this).parent()不会起作用。
            }else{
                alert(‘删除失败’);
            }
        });
    });
});
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!