简体   繁体   English

jQuery.ajax,我总是必须请求两次

[英]jQuery.ajax, I always have to request twice

I am binding the click function foo to the anchor tag in jquery.ajax . 我将click函数foo绑定到jquery.ajax的锚标记。 The problem I am facing is that I have to request twice or click the anchor tag twice to make the request. 我面临的问题是我必须请求两次或单击锚标记两次才能发出请求。 Also I have noticed in the browser under the network bar that ajax request is not triggered, first time when I hit the anchor tag. 我也注意到在网络栏下的浏览器中,当我第一次点击锚标记时,没有触发ajax请求。 But when I hit it twice then I see two ajax request in the network tab. 但是,当我点击两次时,我会在网络标签中看到两个ajax请求。 I have no idea what is going on? 我不知道发生了什么事?

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

    }  

You were binding an extra onclick handler to .remove . 您正在将一个额外的onclick处理程序绑定到.remove

When working with callbacks on dynamically added elements, its better to bind all required events on document context using $(document).on("click", 'selector', function(){ ... }); 在动态添加的元素上使用回调时,最好使用$(document).on("click", 'selector', function(){ ... });将所有必需的事件绑定到document上下文中$(document).on("click", 'selector', function(){ ... }); .

So instead of a href='#' class='remove' onclick='foo(this)' pid='"+ do this (see below snippet) 因此,请执行以下操作(而不是a href='#' class='remove' onclick='foo(this)' pid='"+ )(请参见以下代码段)

$(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");
        }
    });
});

This will separate your HTML layout and its behaviour. 这将分隔您的HTML布局及其行为。

Below things must be avoided (Thanks to @charlietfl): 下面的事情必须避免 (感谢@charlietfl):

  1. Binding 'click' listeners twice on same element. 在同一元素上两次绑定“单击”侦听器。
  2. Using javascript inline with your html code. 在您的html代码中使用内联javascript。
  3. Directly binding a javascript function using onclick attribute. 使用onclick属性直接绑定javascript函数。
  4. Not using javascript unobtrusively. 不要毫不客气地使用javascript。

Hope it helps. 希望能帮助到你。

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

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