简体   繁体   English

在ajax调用之后,onclick jquery无法正常工作

[英]onclick jquery not working after ajax call

I am completely new to jQuery. 我是jQuery的新手。

I have written an onclick event like this: 我写过这样的onclick事件:

     $(document).ready(function () {
         $('body').find('.milestonetable').each(function () {
            var mTable = $(this);
            $(this).find('tr:first').children().eq(0).find('table').find('tr:last').children().eq(0).find('span').on('click', function () {
                var _mid = $(mTable).parent().next('table').find('tbody').find('tr').children().eq(1).find('input').val();
             //some other things to do.
            });

         });
    });

Its working fine. 它的工作正常。 But when I call an ajax, it stops working. 但是当我打电话给ajax时,它就会停止工作。 I have figured out that I have to write it something like this: 我已经发现我必须写这样的东西:

      $(document).on('click','.someclassName', function () {  

       });

But I am unable to convert the above code similar to the code below. 但是我无法转换上面的代码,类似于下面的代码。 Kindly help. 请帮助。

Note: I am using jQuery 1.10.4 注意:我使用的是jQuery 1.10.4

Configuring the event in the following way might overcome your issue. 以下列方式配置事件可能会解决您的问题。

$(document).on("click",".class-added-to-your-span", function(){


});

More details on the live delegation can be found here > http://api.jquery.com/live/ 有关实时授权的更多详细信息,请访问http://api.jquery.com/live/

You may replace your code to this code: 您可以将代码替换为此代码:

$('.milestonetable').on('click', '.class-added-to-your-span', function () {
    var mTable = $(this).closest('.milestonetable');


});

Note that I added a class to the span you are searching for: class-added-to-your-span 请注意,我在您要搜索的范围中添加了一个类: class-added-to-your-span

This way you attach the click event handler to the .milestonetable for your span, and get your code a bit simpler. 这样,您可以将click事件处理程序附加到span的.milestonetable ,并使代码更简单一些。

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

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