简体   繁体   English

jQuery Ajax发布仅工作一次

[英]Jquery ajax post only works once

I have a jquery script that posts a value: 我有一个发布值的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 );
  });
});

This works the first time, but when I click a second time I redirect to my homepage, which makes sense since the anchor tag the .remove class is in has nothing in its href. 第一次有效,但是当我第二次单击时,我重定向到我的主页,这很有意义,因为.remove类所在的锚标记在href中没有任何内容。 However this means the preventdefault() is not working, the entire on click is not working. 但是,这意味着preventdefault()不起作用,整个单击时不起作用。

Why is that? 这是为什么? I googled and found out I should use .on instead of .click but this didn't change anything for me. 我用Google搜索,发现了我应该使用.on代替.click ,但这并没有为我改变什么。

The following html line (which fires the jquery): 以下html行(触发jquery):

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

is in my header.php and shoppingcart.php , when the ajax post is made and the result of shoppingcart.php is loaded in my shopping cart, the link stops working like it should. 在我的header.php和shoppingcart.php ,当进行ajax发布并且将shoppingcart.php的结果加载到我的购物车中时,该链接将停止运行,应有的状态。

What do I need to do? 我需要做什么?

function(EVENT) foggoten in 功能(事件)进入

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM