jQuery Dynamic Button Click Handler
I have a code the change the html of a div to make a button. When I make a click handler for the开发者_Python百科 dynamic button, nothing happens
$('#signinup').html("<button id=\"login_submit\">Sign In</button>");
And the handler:
$('#login_submit').click(function() {
alert("Works!");
});
See the working demo :)
Use the live()
method:
$('#login_submit').live('click', function() {
alert("Works!");
});
The live()
method attach a handler to the event for all elements which match the current selector, now or in the future.
.live
doesn't work always.
Many a times it does, but recently I crashed at a situation wherein I am updating/loading the buttons dynamically which performs some action. Just like you see the Confirm friend request button in facebook friend requests popup flyout.
精彩评论