删除最后一项jquery

时间:2016-05-02 08:06:55

标签: javascript jquery html

点击一个按钮后,结果将附加到另一个div,每次点击总数增加。

单击“删除最后一个”按钮时,将删除附加的div,但不会调整总计。

我一直在找一个没有运气的功能。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Remove last</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">


  </head>
  <body onLoad="renderTime();">
    <div class="container-fluid">
        <div class="row">
            <div class="col-xs-4" id="printarea" >
                <div id="print-container">
                    <div class="item"></div>
                    <div class="total"></div>
                </div>
            </div>
            <div class="col-xs-8">
                <ul class="final">
                    <li class="remove"><a href="#"><input type="submit" value="Remove Last"  class="print-btn remove-item"></a></li>
                    <li class="remove"><a href="#"><input type="submit" value="Remove All" class="print-btn" data-corners="false" id="submit" onclick="refresh()"></a></li>
                </ul>
                <hr />
            </div>
        </div>
        <div class="row">
            <div class="col-xs-12">
                <div class="box">
                    <div class="content">
                        <ul class="sub-menu">
                            <li><a href="#"><button class="menu item1">Item 1</button></a></li>
                            <li><a href="#"><button class="menu item2">Item 2</button></a></li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>


    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script type="text/javascript">

        //add item
        $('.item1').click(function(){
           $('<div class="delete">Item 1 - <span class="skill"> 840</span></div><br />').appendTo('.item');

           counter();
        });
        $('.item2').click(function(){
           $('<div class="delete">Item 2 - <span class="skill"> 910</span></div><br />').appendTo('.item');

           counter();
        });

        //remove last item
        $('.remove-item').click(function(){
            $(".delete:last").remove();
        });
    </script>

    <script type="text/javascript">
        function refresh() {

            setTimeout(function () {
                location.reload()
            }, 100);
        }
    </script>

        <script type="text/javascript">

        var counter = function() {

            var total = 0;

            $(".skill").each(function() {

                total += +$(this).text();
            });

            $('.total').text('Total: ' + total);
        };

    </script>
  </body>
</html>

2 个答案:

答案 0 :(得分:0)

删除元素后,您需要调用counter();方法。

//remove last item
$('.remove-item').click(function() {
    $(".delete:last").remove();

    counter();
});

答案 1 :(得分:0)

这是因为删除元素后没有调用计数器功能。你需要在删除项目点击处理程序中调用它:

 $('.remove-item').click(function(){
        $(".delete:last").remove();
        counter();
 });

在计算总数之前,您还应该在获取文本时解析值:

        var total = 0;

        $(".skill").each(function() {

            total += + parseInt($(this).text() , 10);
        });

        $('.total').text('Total: ' + total);