Jquery ajax帖子只能工作一次

时间:2018-03-07 14:31:33

标签: javascript jquery ajax

我有一个发布值的jquery脚本:

// //Delete a product from the shopping cart
tpj('.remove').on('click', function() {
  // Stop form from submitting normally
  event.preventDefault();
  // Get some values from elements on the page:
  console.log('test');
  var $remove = tpj(this).attr('id');
  url = 'includes/shoppingcart.php';
  // Send the data using post
  var posting = tpj.post( url, { remove: $remove} );
  // Put the results in a div
  posting.done(function( data ) {
    var content = tpj( data );
    tpj( "#result" ).empty().append( content );
  });
});

这是第一次使用,但是当我第二次点击时,我会重定向到我的主页,这是有道理的,因为.remove类所在的锚标记在其href中没有任何内容。但是,这意味着preventdefault()无法正常工作,整个点击工作无效。

为什么?我用Google搜索并发现我应该使用.on而不是.click,但这对我来说没有任何改变。

以下html行(触发jquery):

 <a href="" class="remove" id="'.$cartproduct['product'].'" title="Verwijder product">×</a>

在我的header.php和shoppingcart.php中,当发出ajax帖子并且shoppingcart.php的结果加载到我的购物车中时,链接就会停​​止工作。

我需要做什么?

1 个答案:

答案 0 :(得分:0)

功能(EVENT)foggoten in

    tpj('.remove').on('click', function() {
    //...
    } 
must be:
    tpj('.remove').on('click', function(event) {
    //...
    }