2017-09-14 58 views
-1

我有一个奇怪的问题,从下面的只有第一个作品,我需要提到的是他们都在jQuery(document).ready(function(){}); 控制台中没有错误。jQuery绑定不工作点击

这一个工程

jQuery('.delete_product_line').click(function(){ 
      console.log('xxxxx2'); 
}); 

这一个不工作

jQuery('body').on('click', '.delete_product_line', function(){ console.log('ccccc');}); 

这是一个完整的例子,我知道它没有任何意义,也#submitAddProduct的作品,因为它应该。

jQuery(document).ready(function(){ 



jQuery('body').on('click', '#submitAddProduct', function(){ 


    var interval; 

    var product_id = jQuery('#add_product_product_id').val(); 
    var price_tax_excl = jQuery('#add_product_product_price_tax_excl').val(); 
    var price_tax_incl = jQuery('#add_product_product_price_tax_incl').val(); 
    var qty = jQuery('#add_product_product_quantity').val(); 
    var id_combination = 0; 

    var initial_rows = jQuery('#orderProducts tbody tr').length; 
    var i = 0; 
    var rows = 0; 

    console.log(initial_rows); 
    if(jQuery('#add_product_product_attribute_id').length > 0) 
     id_combination = jQuery('#add_product_product_attribute_id').val(); 

    interval = setInterval(function(){ 

     if(i >= 5000) 
     { 

      clearInterval(interval); 
      interval = 0; 

     } 
     else 
     { 
      rows = jQuery('#orderProducts tbody tr').length; 

      if(rows > initial_rows) 
      { 

       i = 10000; 
       var response_z = $.ajax({ type: "POST", 
          url: aem_ajax, 
          cache: false, 
          data: { action: 'add_product_to_order', id_product: product_id, price_tax_excl: price_tax_excl, price_tax_incl:price_tax_incl, qty:qty, id_combination: id_combination, id_order: aem_id_order, id_employee: aem_id_employee, token: aem_token }, 
          async: true, 
          success: function(data) { 


          } 

          }).responseText; 



      } 


     } 




     i = i + 100; 
    }, 100); 

    return false; 
}); 



jQuery('.delete_product_line').click(function(){ 
      console.log('xxxxx2'); 
}); 

jQuery(document).on('click', '.delete_product_line', function(){ 


    console.log('xxxxx'); 
    var parent = jQuery(this).closest('tr'); 
    var id_order_detail = parent.find('.edit_product_id_order_detail').val(); 

    var price_tax_excl = parent.find('.edit_product_price_tax_excl').val(); 
    var price_tax_incl = parent.find('.edit_product_price_tax_incl').val(); 
    var qty = parent.find('.edit_product_quantity').val(); 
    var id_combination = 0; 

    var link = 'https://mytestsite.com/'+parent.find('a:eq(0)').attr('href'); 
    var url = new URL(link); 
    var id_product = url.searchParams.get("id_product"); 

    var response_z = $.ajax({ type: "POST", 
       url: aem_ajax, 
       cache: false, 
       data: { action: 'remove_product_from_order', id_order_detail: id_order_detail, id_order: aem_id_order, id_product: id_product, id_employee: aem_id_employee,qty: qty, price_tax_excl:price_tax_excl, price_tax_incl:price_tax_incl, token: aem_token }, 
       async: true, 
       success: function(data) { 


       } 

       }).responseText; 

    return false; 
}); 

});

+0

工作对我来说在这里:https://jsfiddle.net/barmar/n3uhe2sy/1/ – Barmar

+0

如果第一个作品,是没有理由的第二个不会:请发表最小的,具体并且可以证实你可以真正重现你的问题的例子。我怀疑代码中有其他干扰逻辑,例如'.off()'或'event.stopImmediatePropagation()'工作。 – Terry

+0

如果它在主体存在之前运行,则第二个将失败(例如在'') –

回答

-1

在这种情况下,使用document代替'body'

jQuery(document).ready(function() { 
    jQuery(document).on('click', '.delete_product_line', function(){ 
     console.log('ccccc'); 
    }); 
}) 
+0

它不起作用。 –

+0

@KevinB你是对的,我只是适应OP的代码。 – Spartacus