如何在关闭时使对话框中的所有活动消失?
我有一个对话框 $('.dialog'),然后在打开它时填充表单的 html:
$('.dialog').html(getForm());
然后我有一个名为 close(); 的函数;当我单击关闭时,我会执行以下操作:
$('.dialog').html('');
$('.dialog').hide();
但是,如果在表单中放置动态元素(新 DOM),则使用 .live('event',function(){})... 当我再次打开对话框时,我会附加两次实时操作,如果我关闭对话框并再次打开,我会附加三次实时操作,等等(请理解我需要使用 .live() 而不是 .bind ())
形式:
<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>
最后,如果我将以下内容放在 .live() 之前,则代码可以正常运行:
$('.please_only_onetime').die();
但是,我想要的是在 close() 函数中概括这一点,例如:
$('.dialog').find('*').die() // but this seems not to be working!
I have a dialog $('.dialog'), 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
或者为了简单起见,您可以继续使用
live
。我认为你应该切换到delegate()
。Or for simplicity you can keep using
live
. I think you should make the switch todelegate()
though.如果您使用“.live()”,则无需重新附加事件处理程序。这就是重点。只需在开始时附加它们,然后无论您加载和卸载表单多少次,它们都会继续工作。
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.