我希望在使用ajax删除之后淡出记录

时间:2017-01-09 06:08:44

标签: javascript ajax

这是我用过的ajax代码。但它不起作用。我希望在删除它之后淡出记录,以便用户可以在不刷新页面的情况下看到它被删除。如果有人可以帮助我真的很棒。谢谢inadvance

function delete_item(item_id){
    var item = item_id;
    var temp = "#" + item_id;
    var temp1 = "'" + temp + "'";

    if(confirm("Sure you want to delete this update? There is NO undo!")){
        $.ajax({
            url : 'DeleteItem.php',
            method : 'POST',
            data : { item: item },
            success : function(){
                $(temp).html(result);
            }      
        });
        $(this).parents("tr").animate({ backgroundColor: "#003" },"slow").animate({ opacity: "hide" }, "slow");
    }         
}

2 个答案:

答案 0 :(得分:0)

对于您制作的每个ajax请求,在执行您真正想要做的动画之前,始终确定它是否成功。尝试在服务器端学习json_encode以获得更好的成功函数结果

<script type="text/javascript">
        function delete_item(item_id){
            var item = item_id;
            var temp = "#"+item_id;
            var temp1 = "'"+temp+"'";
            if(confirm("Sure you want to delete this update? There is NO undo!")){
                $.ajax({
                    url : 'DeleteItem.php',
                    method : 'POST',
                    data : {item:item},
                    success : function(){
                        $(temp).html(result);
                        $(this).parents("tr").animate({ backgroundColor: "#003" }, "slow").animate({ opacity: "hide" }, "slow");
                }
            });
        }
    }
</script>

答案 1 :(得分:0)

<script type="text/javascript">
    function delete_item(item_id){
        var item = item_id;
        var temp = "#"+item_id;
        var temp1 = "'"+temp+"'";
        if(confirm("Sure you want to delete this update? There is NO undo!")){
            $.ajax({
                url : 'DeleteItem.php',
                method : 'POST',
                data : {item:item},
                success : function(){
                    $(temp).fadeOut('fast', function(){
                           // function called after fadeout completes
                           // fadeOut does the animation you want.
                           // you can obtain ID before saving it as a variable
                    }),
                statusCode: {
                    200: function(){
                         // you can put the fadeOut code in here, as this guarantees that when the server responds accordingly you will then run your code, you can also set functions for other response codes.
                       }

                 }
            }
        });
    }
}