function binding and the clone() function - Jquery

后端 未结 3 1101
慢半拍i
慢半拍i 2021-01-14 11:22

I have problems with my keyup binding when cloning an element. Here\'s the scenario:

I have an html markup like this:


          


        
相关标签:
3条回答
  • 2021-01-14 11:33

    You've got two real options

    • use clone(true) which will also clone the bound event handlers
    • use event delegation with live() so that the event handler is bound to a parent element and thus newly added rows will get the same functionality
    0 讨论(0)
  • 2021-01-14 11:34

    Use jQuery's live events; this way the handler will automatically be bound to newly created elements (such as the clones in your example).

    For example:

    $('.rijbasis input').live('keyup', function()
    {
        var parent = $(this).parent().parent();
        $('.total',parent).text(parseInt($('.cost',parent).text()) * parseInt($('.count',parent).val()));
    }
    
    0 讨论(0)
  • 2021-01-14 11:49

    Use .live instead of .keyup

    0 讨论(0)
提交回复
热议问题