简体   繁体   English

如何在Dom中引用模态窗口?

[英]How can i refer to a Modal Window in Dom?

I've gotten hundreds of aids from this site. 我从这个站点获得了数百个帮助。 thanks. 谢谢。 This is my first question. 这是我的第一个问题。 Which object is a modal window (alert popup) into the Dom. 哪个对象是进入Dom的模式窗口(警报弹出窗口)。 How can i refer it? 我该如何参考? How can i know if open or closed? 我怎么知道打开还是关闭? Something like this: if (alertPopup is open) {..code...} My code is this (i use jQuery): 像这样:if(alertPopup已打开){..code ...}我的代码是这样的(我使用jQuery):

<script type="text/javascript">
$(document).ready(function(){
     var myButton = $('#mybutton')

     myButton.click(function(){
        if ($('#myinput').val() == '') {
            alert('input Empty!');
        } else {
            // More code.
        }

     });

     $(document).keyup(function(e){
        if (e.keyCode == 13) myButton.trigger('click');
     })
});
</script>

<body>
    <input id="myinput" />
    <button id="mybutton">Show alert</button>
</body>

The purpose of the code is trigger up the event 'click' on the button whith key 'enter'. 该代码的目的是触发带有“输入”键的按钮“单击”事件。 It works, but when i close the popup, again with key 'enter', the popup comes again an again. 它可以正常工作,但是当我关闭弹出窗口时,再次使用键“ enter”,弹出窗口再次出现。 I need to disable event 'click' of my button or unbind the trigger action when the popup is displayed. 我需要禁用按钮的事件“单击”,或在显示弹出窗口时取消绑定触发动作。 I would't like to make my own modal windows. 我不想创建自己的模态窗口。 thanks in advance. 提前致谢。

You can move the handler to its own function and programmatically bind/unbind it to the event: 您可以将处理程序移至其自己的函数,并以编程方式将其绑定/解除绑定到事件:

$(document).ready(function(){
     var myButton = $('#mybutton')

     console.log('whee');

     myButton.click(clickHandler);

     $(document).keyup(function(e){
        if (e.keyCode == 13) myButton.trigger('click');
     })
});


function clickHandler(){
    $('#mybutton').unbind('click', clickHandler)

    if ($('#myinput').val() == '') {
        alert('input Empty!');
    } else {
        // More code.
    }

}

However, it looks more like you're trying to deal with enter buttons in a form submission style. 但是,它看起来更像是您尝试使用表单提交样式处理回车按钮。 I'd recommend wrapping this whole thing in a form and dealing with it as such. 我建议将整个内容包装成某种形式,并进行相应处理。

See http://jsfiddle.net/ruBY4/ for a cleaner form-based solution. 请参阅http://jsfiddle.net/ruBY4/以获取基于表单的更清洁的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM