javascript - 导航 JSON 对象

标签 javascript jquery 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
        });
});

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

最佳答案

我认为您调用了错误的范围。

也许这可行:

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

关于javascript - 导航 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26090073/

相关文章:

java - 如何在java中使用Map<String, String>构造复杂的json结构

javascript - 遍历嵌套的 JSON 对象并添加属性

javascript - 使用 JavaScript 将 onrightclick 属性添加到 Canvas

javascript - JS : Finding unpaired elements in an array

jquery - 如何使用 jQuery 动态设置宽度和高度

javascript - 使用 jquery 更新页面会使 javascript 函数无用?

javascript - 如何调用 php 函数以使用 jQuery 从 MYSQL 获取更新的数据

javascript - 查找最新更新json的有效算法

javascript - 更改按钮组引导中按钮点击的内容

javascript - 无法访问catch javascript中的变量