开发者

Using the Live method to bind a function to dynamically created checkboxes

开发者 https://www.devze.com 2023-03-03 18:08 出处:网络
How do I开发者_如何学编程 bind the following to all future instances of checkboxes that might be added to my page dynamically. I\'m not sure how to use the Live() method to do this.

How do I开发者_如何学编程 bind the following to all future instances of checkboxes that might be added to my page dynamically. I'm not sure how to use the Live() method to do this.

        $('input:checkbox').imageTickBox({
            tickedImage: "/Content/Img/checkbox_tick.png",
            unTickedImage: "/Content/Img/checkbox_notick.png",
            imageClass: "tickbox"
        });


You cannot do this with .live() (or .delegate()). Those are for binding event handlers for events which may not yet exist.

Description: Attach a handler to the event for all elements which match the current selector, now and in the future.

The image tick box plugin you're using is not any sort of "event." You will have to explicitly call the initialization code (e.g. $('selector').imageTickBox(...)) whenever a new checkbox is added.


.live() is for listening to events and then detecting where those events originated from. Adding elements to the page doesn't trigger anything that .live() can listen to. You'll have to call your plug-in initialization whenever you are adding the checkbox to the page.


This is definitely a duplicate question: Please see

jquery live event for added dom elements

Event binding on dynamically created elements?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号