导航JSON对象

Navigating a JSON object

本文关键字:对象 JSON 导航      更新时间:2023-09-26

此处的总noob:

我得到了一个来自.on('click')函数的JSON结果,它看起来像这样:

{"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
        });
});

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

我认为您调用了错误的作用域。

也许这可以奏效:

$(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(); 
    });
});