简体   繁体   English

在Jquery中任何期望在搜索结果上单击的地方时隐藏搜索结果

[英]Hide search results when clicked anywhere expect on search results in Jquery

I am trying to create a Gmail style search bar. 我正在尝试创建一个Gmail样式的搜索栏。 Basically a search bar which allows user to type search result and display the result in a dropdown style popup. 基本上是一个搜索栏,允许用户键入搜索结果并以下拉样式弹出窗口显示结果。 See image below. 参见下图。

搜索栏

In this design, if I click anywhere in the page, I want to hide the search results. 在这种设计中,如果我单击页面中的任意位置,则要隐藏搜索结果。 But if I click on the search results, I want to instead do some other work (opening a small modal popup, set some session variables etc). 但是,如果我单击搜索结果,我想做一些其他工作(打开一个小模式弹出窗口,设置一些会话变量等)。

Below is the design and event structure I came up with. 下面是我想到的设计和事件结构。

<div onfocusout="return HideSearchResultBox(this,event);">
<input type="text" id="MainPageSearchBar" placeholder="Search here..." oninput="return DisplaySearchResult();" onfocus="return DisplaySearchResultBox();"/>
 <div class="card searchresultcard" style="width:100%">
 <div class="card-body p-2" id="searchResultDisplayDiv">
 </div>
 </div>
 </div>

Here is the code I used to generate the result items. 这是我用来生成结果项的代码。

<div id="SearchBarResult' + someID + '" onclick="return OpenSearchResultTab(' + someID + ');">
<label> DisplayText <span class="badge badge-pill badge-accent" style="float:right">Member</span></label>
<label> SubText</label>
</div>

Basically, I am trying to hide the div with the search results when the main div (including the search box and the results) loses focus, which works fine. 基本上,当主div(包括搜索框和结果)失去焦点时,我试图将div与搜索结果一起隐藏,效果很好。

Problem is that when I click on the search results, the "onfocusout" event of the main div (including the search box and the results) still gets fired. 问题是,当我单击搜索结果时,仍会触发主div的“ onfocusout”事件(包括搜索框和结果)。 So, if I click on a search result, div is closed and the click event of the search result never gets fired. 因此,如果我单击搜索结果,div将关闭,并且搜索结果的click事件将永远不会触发。

I tried few suggestions from stackoverflow itself, that I could find, like moving the "onfocusout" event to the main container div, or using mouseup event. 我尝试了一些stackoverflow本身的建议,例如将“ onfocusout”事件移至主容器div或使用mouseup事件。 But the problem remains the same. 但是问题仍然存在。 I tried to find event target but that comes as null everytime. 我试图找到事件目标,但每次都为null。

Edit: I tried removing the code that hides the search result, and then the click event for search result items work fine. 编辑:我尝试删除隐藏搜索结果的代码,然后搜索结果项的click事件正常运行。 So, if I hide the search result, then click event is not getting fired. 因此,如果我隐藏搜索结果,则不会触发click事件。

you will need to check does mouse is inside the search result when search bar lose the focus, try as follow, 当搜索栏失去焦点时,您需要检查鼠标是否在搜索结果内,请尝试以下操作,

  let flag=false;
//code of on over of search result item
function handleMouseOver(){
 flag=true
}
//code for mouse leave of search result item
function handleMouseLeave(){
 flag=false;
}
//you focus out event of search box
function hadleFocusOut(){
 if(flag){
  //do not hide search result,focus goes to search result item
  //allow user to select search result item
 }else{
  //hide search result
 }
}

You need to check if clicked outside of search area. 您需要检查是否在搜索区域之外单击。

 $(document).on("click touchstart", function(e) { var t = $(e.target).closest('#SearchBoxContainer'); var exceptDiv = $('#SearchBoxContainer'); if (exceptDiv.is(t) == false) { //clicked oustide of div $('#searchResultDisplayDiv').hide(); } }); function DisplaySearchResultBox() { $('#searchResultDisplayDiv').show(); } function doSomething() { alert('Do Something'); } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="SearchBoxContainer"> <input type="text" onfocus="DisplaySearchResultBox();"> <div class="" id="searchResultDisplayDiv" style="display: none;"> <ul style="background:#ccc"> <li onclick="doSomething()">This is search Result</li> <li onclick="doSomething()">This is search Result</li> <li onclick="doSomething()">This is search Result</li> <li onclick="doSomething()">This is search Result</li> <li onclick="doSomething()">This is search Result</li> <li onclick="doSomething()">This is search Result</li> <li onclick="doSomething()">This is search Result</li> </ul> </div> </div> 

In the above, example is a clear instruction to the search DOM element that once the focus is lost close the Search div. 在上面的示例中,对搜索DOM元素的明确说明是一旦失去焦点,就关闭搜索div。 Need to change the approach to achieve the desired functionality. 需要更改方法以实现所需的功能。

One way could be, add an event handler on the document body and check if the click event is triggered not from search results then close the search container. 一种方法可能是,在文档主体上添加事件处理程序,并检查是否不是从搜索结果中触发了click事件,然后关闭了搜索容器。

Hope this helps. 希望这可以帮助。

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

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