为什么我的弹出窗口立即关闭?

时间:2012-10-19 14:11:00

标签: javascript jquery css jquery-ui

我使用Jquery UI在点击时弹出一个弹出窗口但是这个弹出窗口立即关闭。我不明白为什么? 这是我的代码:

function openPopUp() {
    //  alert("Handler for .click() called.");
        $("#dialog:ui-dialog").dialog("destroy");
        $("#select-method").dialog({
            modal : true,
            height: 573,
            width: 766 ,
            buttons : {
                Exporter : function() {
                    //$(this).dialog("close");
                     alert("Exporter");
                    // close the dialog
                },
                'Etat de surveillance' : function() {
                    //$(this).dialog("close");
                     alert("Etat de surveillance");
                    // close the dialog
                },
                Annuler : function() {
                    //$(this).dialog("close");
                     alert("Annuler");
                    // close the dialog
                }
            }
        });
    };

代码html是:

<div id="other" class="popLink">This is text
        <a href="" class="popLink" onClick="openPopUp();">
        Click to open pop up
        </a>
</div>

<div class="noDisplay">
        <div id="select-method" title="selection des méthodes et calcul des provisions">My pop upis opened
            </div>
 </div>

2 个答案:

答案 0 :(得分:3)

就像一条评论所说,尝试这样的事情:

onclick="openPopUp();return false;"

虽然这是有意义的,因为你已经在使用jQuery,用jQuery绑定事件,而不是内联HTML。不要指定onclick属性,请在$(document).ready

中尝试此操作
$(".popLink").click(function (e) {
    e.preventDefault();
    // Either call openPopUp or copy/paste the code from that function into here - depends on how you actually use openPopUp throughout your whole site
});

但与此同时,你的HTML中有一些奇怪的东西 - 具有相同类“popLink”的嵌套div。这意味着如果您使用上面的代码,当您单击内部代码时,showPopUp将执行两次(因为传播)。所以我想我会在技术上使用这种绑定:

$("#other").on("click", ".popLink", function (e) {
    e.preventDefault();
    // Other code
});

这在技术上将click事件绑定到元素#other,但只有在其内部具有“popLink”类的元素引起时才会执行。还有其他几种方法可以定位您想要的<a>,因此这取决于您提供的代码是示例还是真实的。

答案 1 :(得分:0)

试试这个:)

$('.popLink').click(function(e) {
  e.preventDefault();
  openPopUp();
});
相关问题