【问题标题】:jQuery append double postingjQuery追加双重发布
【发布时间】:2013-10-10 12:30:44
【问题描述】:

我有一个发布到 php 页面的 jquery 代码,它回显了一些结果。 当我单击链接并附加数据时,当您单击多次时,数据会加倍。 它完全可以理解为什么这样做(我认为),但是您对如何防止双重附加有任何建议?

代码:

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;
    });
});

【问题讨论】:

    标签: jquery append double


    【解决方案1】:

    如果要覆盖#txtHint中的数据:

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

    ps。我希望这就是你要问的

    【讨论】:

    • 给出了完美的感觉,并且像魅力一样工作 - 非常感谢 :) 不知道为什么我使用 append ^^
    【解决方案2】:

    尝试使用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);
    

    【讨论】:

      【解决方案3】:

      也许您应该通过不允许用户单击来禁用链接并在附加完成时启用..

      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);
         }
      

      【讨论】:

        猜你喜欢
        • 2014-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-26
        • 1970-01-01
        • 2018-09-12
        • 2012-07-12
        • 1970-01-01
        相关资源
        最近更新 更多