如何在关闭时使对话框中的所有活动消失?

发布于 2024-12-07 05:32:49 字数 903 浏览 0 评论 0原文

我有一个对话框 $('.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

客…行舟 2024-12-14 05:32:49
$('.dialog').delegate('.please_only_onetime', 'focusin', function () {
  //this is all you need to do... no rebinding, no "die"'ing ...
});

或者为了简单起见,您可以继续使用live。我认为你应该切换到delegate()

$('.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.

梦境 2024-12-14 05:32:49

如果您使用“.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文