【问题标题】:jQuery.ajax, I always have to request twicejQuery.ajax,我总是要请求两次
【发布时间】:2017-04-09 15:37:00
【问题描述】:

我将点击函数foo 绑定到jquery.ajax 中的锚标记。我面临的问题是我必须请求两次或单击锚标记两次才能发出请求。我也注意到在网络栏下的浏览器中没有触发 ajax 请求,这是我第一次点击锚标记时。但是当我点击它两次时,我在网络选项卡中看到了两个 ajax 请求。我不知道发生了什么事?

 <script type="text/javascript">

function show(thelink)
{

    var cat = thelink.innerHTML;
    var productContainer = document.getElementById("productContainer");

    $.ajax({

           type:"POST",
           url:"fetchdata1",
           data:"cat="+cat,

         success:function(data){
            productContainer.innerHTML ="";
            var $productContainer = $('#productContainer');
            $.each(data,function(key,value){
              if(value['newVar'] === 1)
              {
              $productContainer.append("<div id='productBox' class='grid_3'>\n\
             <a href='product.jsp?id="+value['id']+"'><img src='"+value["image"]+"'/></a><br/>\n\
             <a href='product.jsp?id="+value['id']+"'><span class='black'>"+value['name']+"</span></a><br/>\n\
             <span class='black'>By "+value['company']+"</span><br/><span class='red'>RS."+value['price']+"</span>\n\
             <br/><br/><a href='#' class='remove' onclick='foo(this)' pid='"+value['id']+"'>REMOVE</a></div>");

             }  
             else{

             $productContainer.append("<div id='productBox' class='grid_3'>\n\
             <a href='product.jsp?id="+value['id']+"'><img src='"+value["image"]+"'/></a><br/>\n\
             <a href='product.jsp?id="+value['id']+"'><span class='black'>"+value['name']+"</span></a><br/>\n\
             <span class='black'>By "+value['company']+"</span><br/><span class='red'>RS."+value['price']+"</span></div>"); 
             }
        }) ;

     }      

    });

    return false;
}


  function foo(obj){

            var pid = $(obj).attr("pid");
            $(obj).bind("click", function(){    
                    $.ajax({   
                      type:"POST",
                      url:"removeFromWishlist",
                      data:"pid="+pid,

                      success:function(response){
                        console.log("sent ajax request");
                      }
                    });               
            });

    }  

【问题讨论】:

  • 如果你把它保存在一个文档准备函数中会很好api.jquery.com/ready
  • 将 jQuery click 处理程序绑定到 onclick 处理程序内的元素是没有意义的。这就是为什么您必须单击两次但第二个 clcik 也会绑定一个新的 jquery 侦听器
  • @KishanKumar 你不能把foo() 放在里面准备好...它不会在发生点击的全局空间中可用
  • @charlietfl 那我该怎么办?
  • 在元素上使用类并使用事件委托并停止使用内联javascript

标签: javascript jquery ajax


【解决方案1】:

您将一个额外的 onclick 处理程序绑定到 .remove

在对动态添加的元素使用回调时,最好使用$(document).on("click", 'selector', function(){ ... });document 上下文中绑定所有必需的事件。

所以不是a href='#' class='remove' onclick='foo(this)' pid='"+ 这样做(见下面的sn-p)

$(document).on("click", '#productBox .remove', function(){
    // your ajax call
    $.ajax({   
        type:"POST",
        url:"removeFromWishlist",
        data:"pid="+pid,
        success:function(response){
            console.log("sent ajax request");
        }
    });
});

这将分离您的 HTML 布局及其行为。

必须避免以下事情(感谢@charlietfl):

  1. 在同一元素上绑定“点击”侦听器两次。
  2. 在您的 html 代码中使用内嵌 javascript。
  3. 使用onclick属性直接绑定javascript函数。
  4. 不要不显眼地使用 javascript。

希望对你有帮助。

【讨论】:

  • 没有解释原因,也没有建议不要使用内联脚本来支持不显眼的脚本。这是 2017 年而不是 1997 年
  • 我想我必须绑定它,因为我在 jQuery.Ajax 中创建的 html 无法识别 onclick 函数,因为它是动态 html。
  • @charlietfl,当然,如果有人问我,我会建议的。我刚刚指出了现有脚本可能出错的地方
  • OP 显然 尝试 使用不显眼的方法....但是您告诉他们不要这样做。这就是你自己编码的方式吗?
  • @charlietfl 我通常在动态元素上使用$(document).on("click", func...)} 绑定事件,是的,我避免使用onclick 内联元素
猜你喜欢
  • 2014-05-13
  • 1970-01-01
  • 1970-01-01
  • 2016-09-05
  • 2013-06-25
  • 2015-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多