简体   繁体   English

在表单字段外部单击时隐藏自动填充下拉菜单

[英]Hide autofill dropdown on click outside of form field

I made an autofill search where when you type into the form field, it'll populate a dropdown of results that are from an array. 我进行了自动填充搜索,当您在表单字段中键入内容时,它将填充来自数组的结果的下拉列表。

The issue I'm having is that when you click outside of the dropdown, it disappears as it should, but when you start typing into the form field again, the dropdown won't show. 我遇到的问题是,当您在下拉列表之外单击时,它会按原样消失,但是当您再次开始在表单字段中键入内容时,该下拉列表将不会显示。 I'm trying to use jQuery to achieve this: 我正在尝试使用jQuery实现此目的:

This is to display the dropdown under the search field: 这是在搜索字段下显示下拉列表:

const input = document.querySelector('#FAVORITE_LOCATION');
const suggestions = document.querySelector('.suggestions ul');
const country = ['USA', 'Canada'];

function search(str) {
    let results = [];
    const val = str.toLowerCase();

    for (i = 0; i < country.length; i++) {
        if (country[i].toLowerCase().indexOf(val) > -1) {
            results.push(country[i]);
        }
    }

    return results;
}

function searchHandler(e) {
    const inputVal = e.currentTarget.value;
    let results = [];
    if (inputVal.length > 0) {
        results = search(inputVal);
    }
    showSuggestions(results);
}

function showSuggestions(results) {

    suggestions.innerHTML = '';

    if (results.length > 0) {
        for (i = 0; i < results.length; i++) {
            suggestions.innerHTML += `<li>${results[i]}</li>`;
        }
        suggestions.classList.add('has-suggestions');
    } else {
        results = [];
        suggestions.innerHTML = '';
        suggestions.classList.remove('has-suggestions');
    }
}

function useSuggestion(e) {
    input.value = e.target.innerText;
    input.focus();
    suggestions.innerHTML = '';
    suggestions.classList.remove('has-suggestions');
}

input.addEventListener('keyup', searchHandler);
suggestions.addEventListener('click', useSuggestion);

Below is where I'm trying to hide the autofill that gets populated: 下面是我尝试隐藏填充的自动填充的位置:

$(document).on('click', function(event){
    var container = $("ul.has-suggestions");
    if (!container.is(event.target) &&            
        container.has(event.target).length === 0) 
    {
       container.hide();
    }else{
        container.show();
    }
});

Lastly, here's the CSS for the dropdown itself 最后,这是下拉菜单本身的CSS

.search-container {
  position: relative;
}

.search-container input,
.search-container .suggestions {
  width: 100%;
}
.search-container input {
  background-color: white;
  height: 60px;
  padding: 0 10px;
}
.search-container .suggestions {
  position: absolute;
  top: 80px;
}

ul {
  display: none;
  list-style-type: none;
  padding: 0;
  margin: 0;
  box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.2);
  max-height: 200px;
  overflow-y: auto;
}
ul.has-suggestions {
  display: block;
}
ul li {
  padding: 10px;
  cursor: pointer;
  background-color: white;
  color: black;
}

ul.has-suggestions is set to display:block when the user starts to type into the form field, which then shows a populated dropdown. 当用户开始在表单字段中键入内容时,ul.has-suggestions设置为display:block,然后显示填充的下拉列表。 I tried to use an if/else statement and using .show(); 我试图使用if / else语句并使用.show(); but it doesn't seem to work. 但它似乎不起作用。

Any help would be appreciated! 任何帮助,将不胜感激!

Thank you! 谢谢!

You probably want to do something like this to show and/or hide your autocomplete suggestions: 您可能想要执行以下操作来显示和/或隐藏自动完成建议:

input.addEventListener('blur', function() {
  $("ul.has-suggestions").hide();
})

input.addEventListener('focus', function() {
  $("ul.has-suggestions").show();
})

edit: codepen with example 编辑: 带示例的codepen

edit 2 with requested update; 编辑2并请求更新; codepen with example Codepen的例子

This second codepen populates the form field with the clicked value. 第二个Codepen用单击的值填充表单字段。

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

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