2014-11-06 123 views
0

所以,我有以下功能。单击我发送一些数据通过AJAX到填充页面,我张贴到我的分贝。这部分工作正常。但是,我需要删除我的表中成功的数据库插入行。我试图使用.done(),但由于某种原因,它不会触发。是否有触发器,我需要执行另一个PHP页面,让这个脚本知道这是一个成功的操作?AJAX回调不会触发

$(document).on('click', '.send', function() { 
    $(this).closest('tr').find('input:not(.ofrCode)').each(function(){ 
     if ($(this).val() == '') { 
      $(this).parent().addClass('has-error'); 
     } 
    }); 
    if (!$(this).closest('tr').find('td').hasClass('has-error')) { 

     var code = $(this).closest('tr').find('.item').val(); 

     $.ajax({ 
      type: 'POST', 
      url: '/myPage.php', 
      data: { 
       'code' : code 
      }, 
      dataType : 'json', 
      async: false 

     }).done(function() { 

      $(this).closest('tr').remove(); 

     }); 
    } 
}); 
+2

'async:false' ---你为什么需要这个? HTTP请求是否成功执行? – zerkms 2014-11-06 22:57:49

+0

1)为什么'sync:false'和2)尝试添加'.fail(function(data){console.log(“error”,data);})'看看它是否失败 – elzi 2014-11-06 22:58:25

+2

请注意,使用'.done )'async:false'从jQuery 1.8开始不推荐使用。 – Barmar 2014-11-06 23:02:37

回答

0

OK,结合@Papa和@elzi的建议,我得到它通过改变工作如下:

var src = $(this), 
    code = src.closest('tr').find('.item').val(); 

$.ajax({ 
     type: 'POST', 
     url: '/myPage.php', 
     data: { 
      'code' : code 
     }, 
     dataType : 'json', 
     statusCode: { 
      200: function() { 
       src.closest('tr').remove(); 
      } 
     } 

}); 
0

$(this)不正确,因为在返回ajax调用时,上下文不会相同。

在调用ajax之前,您需要将this保存到变量中。

var source = $(this); 

在回调的AJAX应用:

source.closest('tr').remove(); 
+0

嗯...并没有真正的帮助。抱歉。 – santa 2014-11-07 01:28:02