2014-09-28 39 views
0

总小白在这里:导航JSON对象

我有一个JSON结果来到了一个.on('click')功能,看起来像这样:

{"1411939719-8696.jpeg":true} 

我想删除表中的一行,立足在电话会议上来自哪里,但由于某种原因,它不工作:

$('#fileupload').fileupload({ 
    dataType: 'json', 
    done: function (e, data) { 
     $.each(data.result.files, function (index, file) { 
      $('<p/>').text(file.name).appendTo(document.body); 
      var del = $('<button/>') 
       .addClass('btn btn-danger') 
       .attr('data-type', 'DELETE') 
       .attr('data-url', file.deleteUrl) 
       .text('DELETE'); 


       var thumb = $('<img />', 
        { id: file.thumbnailUrl+'_ID', 
        src: file.thumbnailUrl, 
        alt:'MyAlt'}); 

       $('#preview').find('tbody') 
       .append($('<tr>') 
        .append($('<td>').append(thumb)) 
        .append($('<td>').append(del)) 
       ); 
       }); 


      } 
    }); 

和上点击功能是在这里:

$(document).on("click", ".btn-danger", function() { 
$.ajax({ 
    url: $(this).attr("data-url"), 
    type: $(this).attr("data-type") 
}).done(function (result) { 
      $(this).closest("tr").remove(); // to remove the complete row after delete success 
     }); 
}); 

我需要删除包含删除按钮的行以及缩略图图像,但此代码不起作用?

回答

1

我想你是在调用错误的范围。

也许这可以工作:

$(document).on("click", ".btn-danger", function() { 
    //save scope-object to that 
    var that = this; 
    $.ajax({ 
    url: $(this).attr("data-url"), 
    type: $(this).attr("data-type") 
    }).done(function (result) { 
    $(that).closest("tr").remove(); 
    }); 
}); 
+0

如果你可以让我知道我可以创建基于结果是否== TRUE条件,这将是惊人的? – Mobaz 2014-09-28 22:15:18

+0

我理解你了吗,回复是这样的:'{“1411939719-8696.jpeg”:true}'? – 2014-09-28 22:19:30

+0

是的,这就是它 - 第一部分是被删除的文件的文件名,并从服务器重新启动 – Mobaz 2014-09-28 22:26:06