开发者

How to DIE all LIVE'S in a dialog when close?

I have a dialog $('.di开发者_JAVA技巧alog'), then a fill with an html of the form when I open it:

$('.dialog').html(getForm());

Then I have a function named close(); When I click close I do:

$('.dialog').html('');
$('.dialog').hide();

BUT, if in the form I put dynamic elements (new DOM), with .live('event',function(){})... when I open the dialog again I get the live action attached two times, if I close the dialog and open again I get the live action attached three times, etc. (Please UNDERSTAND that I need to use .live() and not .bind())

THE FORM:

<form id="formid">
<input type="text" class="please_only_onetime"/>
<script type="text/javascript">
$(document).ready( function() {
  $('.please_only_onetime').live('focusin', function(){ alert( 'one time' );});
});
</script>
</form>

Finally, if I put the following before the .live(), the code works well:

$('.please_only_onetime').die();

BUT, What I want is to generalize this in close() function like:

$('.dialog').find('*').die() // but this seems not to be working!


$('.dialog').delegate('.please_only_onetime', 'focusin', function () {
  //this is all you need to do... no rebinding, no "die"'ing ...
});

Or for simplicity you can keep using live. I think you should make the switch to delegate() though.


If you're using ".live()", you don't have to re-attach the event handlers. That's the whole point. Just attach them at the start, and then they'll keep working no matter how many times you load and unload the form.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜