简体   繁体   English

带表格的onclick下拉菜单,点击外面关闭

[英]onclick dropdown menu with a form, click outside to close

ive got a drop down menu with form elements. 我有一个带有表单元素的下拉菜单。 the menu element gets set to menu元素设置为

display:none 

when anything outside the element is clicked ... or at least so i thought. 当点击元素之外的任何东西时...或至少我认为。

Here's my little function 这是我的小功能

$(function(){
    $('#loginAcc').click( function(event){
        event.stopPropagation();
        //show
        $('#auth-menu').css('display', 'block');
    });

    $(document).click( function(){
        //hide when click anywhere out the menu
        $('#auth-menu').hide();
    });
})

the problem i have is that it also closes when i click inside the element, which makes it very difficult, pretty much impossible to complete a form. 我遇到的问题是,当我在元素内部单击时它也会关闭,这使得完成表单变得非常困难。

#loginAcc

is a horizontal list item that gets clicked, and 是一个单击的水平列表项,和

#auth-menu

is

if i were to hazard a guess, i would like to think that .toggle() is the culprit, but that's a sheer guess and i wouldn't even know where to start if i were to reimplement it (a little bird told me that it's getting taken out of the spec anyway). 如果我冒险猜测,我想认为.toggle()是罪魁祸首,但这是一个纯粹的猜测,如果我重新实现它我甚至不知道从哪里开始(一只小鸟告诉我,它无论如何都会脱离规范)。

what i would like to happen is that when you click on the #loginAcc list item, the #auth-menu gets set to display:block, and can only be closed if you reclick #loginAcc or anywhere else outside of #auth-menu. 我想要发生的是,当你点击#loginAcc列表项时,#auth-menu设置为display:block,只有当你点击#loginAcc或#auth-menu之外的任何地方时才能关闭它。

any help would be amazing. 任何帮助都会很棒。 thanks 谢谢

Use a not() selector to exclude the menu: 使用not()选择器排除菜单:

$(document).not('#auth-menu').click( function(){
    //hide when click anywhere out the menu
    $('#auth-menu').hide();
});

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

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