对动态创建的元素的事件绑定不起作用

时间:2015-04-08 19:42:24

标签: jquery dynamic bind element

我正在购买购物车,我会在会话中存储好信息 我使用ajax在html页面中创建购物车,这样我可以添加好购物车并且它可以正常工作。但是当我想从购物车中删除它时它无法正常工作。另外,我必须刷新页面到删除好处。
我的代码如下:
cart.php

 class CartController extends Controller {
    public function __construct(){
        parent::__construct();

        // session(null);
        if(!isset($_SESSION['cart'])){
            $_SESSION['cart'] = array();
        }
    }

    public function index(){
        kd($_SESSION);
    }

    public  function addItem(){
        /*
         * TODO --- 安全方面的考虑
         */
        $good_id = intval(I('post.good_id'));

        if(isset($_SESSION['cart'][$good_id])){
            $_SESSION['cart'][$good_id]['good_num'] += 1;
            $html = '';
            $total_price = 0;
            foreach($_SESSION['cart'] as $k=>$v){
                $total_price += $v['good_price']*$v['good_num'];
                $html .= '<div class="shoppingCartProductWrapper"> <a  href="' . U('mall/index/gooddetail',array('id'=>$v['good_id'])) . '" class="shoppingCartProductImageWrapper">'.'<img src="'.$v['good_image'].'" class="shoppingCartProductImage" alt=""/></a><div class="shoppingCartProductInfoWrapper"> <a href="'.U('mall/index/gooddetail',array('id'=>$v['good_id'])).'" class="shoppingCartProductTitle">'.$v['good_name'].'</a><div class="shoppingCartProductButtonsWrapper"><input type="text" id="shoppingCartProductNumber-1" class="shoppingCartProductNumber" name="product-1" value="'.$v['good_num'].'" /><span class="shoppingCartProductPrice">'.$v['good_price'].'</span><a href="javascript:void(0)" class="shoppingCartRemoveProductButton"></a> </div></div></div>';
            }
            $html .= '<div class="shoppingCartInfoWrapper"><span class="shoppingCartProductsNumber">商品数量:'.count($_SESSION['cart']) . '</span><span class="shoppingCartProductsTotal">Total: ¥'.$total_price.'</span></div>';
            exit($html);
        }
        $good_name = I('post.good_name');
        $good_price = I('post.good_price');        
        $good_image = I('post.good_image');
        $good_num = intval(I('post.good_num'));
        $good = compact('good_id', 'good_name', 'good_price', 'good_image', 'good_num');
        $_SESSION['cart'][$good_id] = $good;

        $html = '';
        $total_price = 0;
        foreach($_SESSION['cart'] as $k=>$v){
            $total_price += $v['good_price']*$v['good_num'];
            $html .= '<div class="shoppingCartProductWrapper"> <a  href="' . U('mall/index/gooddetail',array('id'=>$v['good_id'])) . '" class="shoppingCartProductImageWrapper">'.'<img src="'.$v['good_image'].'" class="shoppingCartProductImage" alt=""/></a><div class="shoppingCartProductInfoWrapper"> <a href="'.U('mall/index/gooddetail',array('id'=>$v['good_id'])).'" class="shoppingCartProductTitle">'.$v['good_name'].'</a><div class="shoppingCartProductButtonsWrapper"><input type="text" id="shoppingCartProductNumber-1" class="shoppingCartProductNumber" name="product-1" value="'.$v['good_num'].'" /><span class="shoppingCartProductPrice">'.$v['good_price'].'</span><a href="javascript:void(0)" class="shoppingCartRemoveProductButton"></a> </div></div></div>';
        }
        $html .= '<div class="shoppingCartInfoWrapper"><span class="shoppingCartProductsNumber">商品数量:'.count($_SESSION['cart']) . '</span><span class="shoppingCartProductsTotal">Total: ¥'.$total_price.'</span></div>';

        exit($html);
    }

    public function removeItem(){
        $good_id = intval(I('post.good_id'));
        unset($_SESSION['cart'][$good_id]);

        $html = '';
        $total_price = 0;
        foreach($_SESSION['cart'] as $k=>$v){
            $total_price += $v['good_price']*$v['good_num'];
            $html .= '<div class="shoppingCartProductWrapper"> <a  href="' . U('mall/index/gooddetail',array('id'=>$v['good_id'])) . '" class="shoppingCartProductImageWrapper">'.'<img src="'.$v['good_image'].'" class="shoppingCartProductImage" alt=""/></a><div class="shoppingCartProductInfoWrapper"> <a href="'.U('mall/index/gooddetail',array('id'=>$v['good_id'])).'" class="shoppingCartProductTitle">'.$v['good_name'].'</a><div class="shoppingCartProductButtonsWrapper"><input type="text" id="shoppingCartProductNumber-1" class="shoppingCartProductNumber" name="product-1" value="'.$v['good_num'].'" /><span class="shoppingCartProductPrice">'.$v['good_price'].'</span><a href="javascript:void(0)" class="shoppingCartRemoveProductButton"></a> </div></div></div>';
        }
        $html .= '<div class="shoppingCartInfoWrapper"><span class="shoppingCartProductsNumber">商品数量:'.count($_SESSION['cart']) . '</span><span class="shoppingCartProductsTotal">Total: ¥'.$total_price.'</span></div>';
        exit($html);
    }
}

shoppcart.html

<!-- shopping cart wrapper start -->
<form id="shoppingCartWrapper" class="shoppingCartWrapper" action="#" method="post">
  <fieldset>        
    <?php 
      $total_price = 0;
      foreach($_SESSION['cart'] as $k=>$v){
        $total_price += $v['good_price']*intval($v['good_num']);
    ?>
    <!-- shopping cart product starts -->
    <div class="shoppingCartProductWrapper"> 
      <a href="<?php echo DOMAIN.'/mall/index/gooddetail/id/'.$v['good_id'].'.html'; ?>" class="shoppingCartProductImageWrapper">
        <img src="<?php echo $v['good_image']; ?>" class="shoppingCartProductImage" alt=""/>
      </a>
      <div class="shoppingCartProductInfoWrapper"> 
        <a href="<?php echo DOMAIN.'/mall/index/gooddetail/id/'.$v['good_id'].'.html'; ?>" class="shoppingCartProductTitle"><?php echo $v['good_name']; ?></a>
        <div class="shoppingCartProductButtonsWrapper">
          <input type="text" class="shoppingCartProductNumber" name="product-3" value="<?php echo $v['good_num']; ?>"/>
          <input type="hidden" name="good_id" value="<?php echo $v['good_id']; ?>" />
          <span class="shoppingCartProductPrice">¥<?php echo $v['good_price']; ?></span><a href="javascript:void(0)" class="shoppingCartRemoveProductButton"></a> </div>
      </div>
    </div>
    <!-- shopping cart product ends --> 
    <?php } ?>

    <!-- shopping cart info wrapper starts -->
    <div class="shoppingCartInfoWrapper"> 
      <span class="shoppingCartProductsNumber">商品数量: <?php echo count($_SESSION['cart']); ?></span> 
      <span class="shoppingCartProductsTotal">Total: ¥<?php echo $total_price; ?></span> 
    </div>
    <!-- shopping cart info wrapper ends -->

    <div id="shoppingCartButtons" class="shoppingCartButtonsWrapper"><a href="" class="shoppingCartEmptyButton">清空</a>
      <input type="submit" value="去付款" id="shoppingCartCheckoutButton" class="shoppingCartCheckoutButton"/>
    </div>
  </fieldset>
</form>
<!-- shopping cart wrapper ends --> 
<script type="text/javascript">
    $(function(){
      remove_url = "{:U('mall/cart/removeitem')}";
      $('#shoppingCartWrapper').find('.shoppingCartRemoveProductButton').on('click',function(){
        good_id = $(this).prev().prev().val();
        $.ajax({
          'url': remove_url,
          'type': 'post',
          'data': {'good_id': good_id},
          'success': function(data){
                $('#shoppingCartButtons').prevAll().remove();
                $('#shoppingCartButtons').before(data);
          }
        });
      });
    });
</script>


抱歉我的英文不好!希望我能清楚地描述我的问题,如果没有,我也将代码放在网上,地址为:http://weixin.chenfw.me/mall/index
请帮帮我......

1 个答案:

答案 0 :(得分:0)

问题是移除事件的选择器 你想点击删除按钮,但这个按钮是通过dom而不是文档加载出现的。

尝试使用更改选择器来记录侦听事件。单击selector_added。

$('#shoppingCartWrapper').find('.shoppingCartRemoveProductButton').on('click',fu‌​nction(){ ...

 $(document).on('click', '#shoppingCartWrapper .shoppingCartRemoveProductButton', function() {...

如果我尝试在控制台中运行你的ajax,它可以正常工作。