第二步删除对象项时出错

时间:2014-07-24 02:41:37

标签: javascript jquery

   $("body").on("click", ".delete_human", function(){
        var custom_tdid = $(this).attr('data-id');
        $(".peoples_data .xls-people-"+custom_tdid).hide(400 , function() {$(".peoples_data .xls-people-"+custom_tdid).remove()});
        console.log(people);
        for(var i = 0; i < people.length; i++){
            if(people[i].tdid==custom_tdid)
            {
                delete people[i];
                delete people_to_parse[i];
                delete people_to_save_parse[i];
            }
        }
        console.log(people); 
    });

Morkup:

        var td1 = "<tr class='xls-people-"+custom_tdid+"'><td><div class='parse_name Photo-text-"+custom_tdid+"'><img src='https://graph.facebook.com/3/picture'/></div></td>";
        var td2 = "<td><div class='parse_name ID-text-"+custom_tdid+"'>"+custom_id+"</div><div class='parse_name1'><div class='switch demo3'><input type='checkbox' id='counters' class='ID-"+custom_tdid+"'><label><i></i></label></div></div></td>";
        var td3 = "<td><div class='parse_name Name-text-"+custom_tdid+"'>"+custom_firstname+"</div><div class='parse_name1'><div class='switch demo3'><input type='checkbox' id='counters' class='Name-"+custom_tdid+"'><label><i></i></label></div></div></td>";
        var td4 = "<td><div class='parse_name Surname-text-"+custom_tdid+"'>"+custom_lastname+"</div><div class='parse_name1'><div class='switch demo3'><input type='checkbox' id='counters' class='Surname-"+custom_tdid+"'><label><i></i></label></div></div></td>";
        var td5 = "<td><div class='parse_name Nickname-text-"+custom_tdid+"'>"+custom_nickname+"</div><div class='parse_name1'><div class='switch demo3'><input type='checkbox' id='counters' class='Nickname-"+custom_tdid+"'><label><i></i></label></div></div></td>";
        var td6 = "<td><div class='parse_name Phone-text-"+custom_tdid+"'>"+custom_phone+"</div><div class='parse_name1'><div class='switch demo3'><input type='checkbox' id='counters' class='Phone-"+custom_tdid+"'><label><i></i></label></div></div></td>";
        var td7 = "<td><div class='parse_name City-text-"+custom_tdid+"'>"+custom_city+"</div><div class='parse_name1'><div class='switch demo3'><input type='checkbox' id='counters' class='City-"+custom_tdid+"'><label><i></i></label></div></div></td>";
        var td8 = "<td><div class='parse_name CountFriends-text-"+custom_tdid+"'>"+custom_countfriends+"</div><div class='parse_name1'><div class='switch demo3'><input type='checkbox' id='counters' class='CountFriends-"+custom_tdid+"'><label><i></i></label></div></div></td>";
        var td9 = "<td><div class='parse_name Mail-text-"+custom_tdid+"'>"+custom_mail+"</div><div class='parse_name1'><div class='switch demo3'><input type='checkbox' id='counters' class='Mail-"+custom_tdid+"'><label><i></i></label></div></div></td>";
        var td10 = "<td><div class='parse_name Avocation-text-"+custom_tdid+"'>"+custom_avocation+"</div><div class='parse_name1'><div class='switch demo3'><input type='checkbox' id='counters' class='Avocation-"+custom_tdid+"'><label><i></i></label></div></div></td>";
        var td11 = "<td><div class='parse_name Status-text-"+custom_tdid+"'>Wait to parse</div><button class='delete_human' data-id='"+custom_tdid+"'>DELETE</button></td></tr>";
        $(".peoples_data tbody").append(td1+td2+td3+td4+td5+td6+td7+td8+td9+td10+td11).hide().show('slow');

enter image description here

2 个答案:

答案 0 :(得分:1)

问题是delete运算符删除元素,而是使用splice,因为delete仍然保留索引值为undefined,但是splice实际上从数组中删除元素

$("body").on("click", ".delete_human", function(){
    var custom_tdid = $(this).attr('data-id');
    $(".peoples_data .xls-people-"+custom_tdid).hide(400 , function() {$(".peoples_data .xls-people-"+custom_tdid).remove()});
    console.log(people);
    for(var i = 0; i < people.length; i++){
        if(people[i].tdid==custom_tdid)
        {
            people.splice(i, 1);
            people_to_parse.splice(i, 1);
            people_to_save_parse.splice(i, 1);
        }
    }
    console.log(people); 
});

<强>调试

你可以看到(在你的控制台输出中)数组中实际上有两个元素,但长度仍然保持值3,现在你可以意识到错误发生的位置。

答案 1 :(得分:0)

你的for循环约束是people.length。不能保证people_to_parse和people_to_save与人的长度相同。