Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jqueryajax成功问题_Jquery - Fatal编程技术网

jqueryajax成功问题

jqueryajax成功问题,jquery,Jquery,为什么以下脚本通过删除relievant html实体在客户端工作: $(".ui-delete").click(function() { $.ajax({ url: 'delete.aspx', type: 'POST', data: { strWidgetID:$(this).parents(".widget").attr("id") }, error: function() { alert('Error'); },

为什么以下脚本通过删除relievant html实体在客户端工作:

$(".ui-delete").click(function() {

    $.ajax({
        url: 'delete.aspx',
        type: 'POST',
        data: { strWidgetID:$(this).parents(".widget").attr("id") },
        error: function() { alert('Error'); },
        success: function() { alert('Success'); }
    });


    $(this).parents(".widget:first").remove();
});
但是下面这个“更合适”的查询通过删除html实体不起作用

$(".ui-delete").click(function() {

    $.ajax({
        url: 'delete.aspx',
        type: 'POST',
        data: { strWidgetID:$(this).parents(".widget").attr("id") },
        error: function() { alert('Error'); },
        success: function() {
            alert('Success');
            $(this).parents(".widget:first").remove();
        }
    });

});
第一个脚本正确执行clientside和serverside,第二个脚本正确执行serverside,但在clientside上,它只显示一个警报“success”,但不删除html实体“widget”


有什么想法吗?

在成功处理程序中,
这不是单击处理程序中的内容(它是$.ajax使用的XMLHttpRequest对象)

在$.ajax调用之前,捕获对您感兴趣的
的引用:

$(".ui-delete").click(function() {
  var that = this;

  $.ajax({
    // etc.
    success: function() {
      $(that).parents('.widget:first').remove();
    }
  });
};
根据上下文,$(this)引用不同的对象。查看此链接 在第二个代码示例中,这指的是ajax设置对象,而不是“ui delete”元素。

--将代码缩进四个空格,以便正确呈现。因为你是新来的(欢迎!),我已经为你修好了。$(这个)可能不是你想象的那样。在那里放置一个断点并检查。