简体   繁体   English

如何将 select 框与 jQuery 而不是 select 框内的形式?

[英]How to select a box with jQuery but not select the form inside the box?

I have a code where I want the page to close when the box is clicked with the "box-search" class But even though I said don't consider the form and the elements inside the form, the form will be closed when the form and input are clicked.我有一个代码,我希望在使用“框搜索”单击框时关闭页面 class 但是即使我说不考虑表单和表单内的元素,表单也会在表单时关闭和输入被点击。

 $(".box-search *").not('form, form *').click(function() { $(".box-search").fadeOut(); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="box-search"> <div class="container" style="font-size: 40px; color: red;"> <button type="submit" class="close-box-search">Close</button> <form method="get" action=""> <input type="text"> <button type="submit">Search</button> </form> </div> </div>

I found the answer to my question To select the parent, but not apply to the child We use the following code我找到了问题的答案 To select the parent, but not apply to the child 我们使用下面的代码

$(".box-search").click(function (e) { 
    if( $(e.target).hasClass("box-search") || $(e.target).hasClass("container") || $(e.target).hasClass("close-box-search") ){ 
        hideBoxSearch()
    }
});

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

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