2013-10-10 44 views
0

我有一个jQuery代码发布到一个php页面,这是回应一些结果。 当我点击一个链接并追加数据时,当您单击多次时,它会使数据翻倍。 它完全理解为什么它会这样(我认为),但你有什么建议如何防止双重追加发生?jquery append double posting

代码:

jQuery(function(){ 
    jQuery('.link').click(function(){ 
     var id = jQuery(this).attr('id'); 

     jQuery.ajax({ 
      url : "medialib/getpictures.php", 
      data: "linkid="+id, 
      type : "post", 
      success: function(html){ 
       jQuery('#txtHint').append(html); 
     } 

     }); 
    return false; 
    }); 
}); 

回答

2

如果你想在#txtHint覆盖数据:

jQuery('#txtHint').html(html); 

PS。我希望这是你问

+0

完全给人感觉和工作就像一个魅力 - 非常感谢:) 不知道为什么我用这个^^附加 – simon

0

尝试使用html()

jQuery('#txtHint').html(html); 

如果你想保持以前的数据,然后尝试像,

prevHTML=jQuery('#txtHint').html(); 
newHTML=prevHTML.replace(html,'')+html; // replace previous html if same html comes in 
jQuery('#txtHint').html(newHTML); 
0

也许你应该禁用该链接通过不允许用户点击它并在添加完成后启用。

jQuery(function(){ 
    jQuery('.link').click(function(){ 
     var id = jQuery(this).attr('id'); 
     //diable .link click here 
     jQuery.ajax({ 
      url : "medialib/getpictures.php", 
      data: "linkid="+id, 
      type : "post", 
      success: function(html){ 
       jQuery('#txtHint').append(html); 
       //enable it again here 
     } 

     }); 
    return false; 
    }); 
}); 

或者你也可以使用HTML()函数简单地替换现有的内容,

这样,

success: function(html){ 
     jQuery('#txtHint').html(html); 
    }