点击jQuery对话框中的事件会发生两次? [英] Click events in a jQuery Dialog occur twice?

查看:70
本文介绍了点击jQuery对话框中的事件会发生两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在处理一个奇怪的问题,即每当发生在jQuery对话框中时,事件会发生两次, .click()

I have been dealing with an odd problem that .click() events happen twice whenever placed in a jQuery Dialog.

我的简单测试用例在下面,现场示例在这里

My simple test case is below and a live example is here

  <div id="popup" style="display: none">
    <a href="javascript:void(0);" id="testlink">Test Link</a>
    <script type="text/javascript">
      $('#testlink').click(function(){
        alert("Test Link clicked");
        return 0;
      });
    </script>
  </div>
  <script type="text/javascript">
  $(document).ready(function(){
    $('#popup').css('display','block');
    var h=($(window).height()+0.0)*0.9;
    var w=($(window).width()+0.0)*0.9;
    if(w >= 800){
      w = 800;
    }
    $('#popup').dialog({
      autoOpen: true,
      width: w,
      height: h,
      modal: true,
      open: function(event,ui){
        $('body').css('overflow', 'hidden');
      },
      close: function(event,ui){
        $('body').css('overflow', 'scroll');
      }
    });
  });
  </script>


推荐答案

移动< script> ; 块,注册在弹出 div之外的点击事件,我认为当div变得可见时,JS被另一次解析...

Move the <script> block that registers the click event outside of the popup div, I think the JS gets parsed another time when the div becomes visible...

这篇关于点击jQuery对话框中的事件会发生两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆