【发布时间】:2017-08-03 07:35:03
【问题描述】:
我希望在我的 Shopify 商店中的搜索栏上设置自动完成功能 - 这使用 Liquid 和 ajax
我找到了这个教程,并按照它说的实现了它,但是它不起作用,网站上的任何搜索栏都没有自动完成 - 我认为它可能很旧并且是在更新/更改为 Minimal/ 之前编写的Shopify 搜索功能。
https://help.shopify.com/themes/customization/store/enable-autocomplete-for-search-boxes
我可以使用 Chrome 开发工具完成它,它似乎卡在添加搜索结果列表 $('<ul class="search-results"></ul>').appendTo($(this)).hide(); 的位置,这在跟踪页面的 HTML 时不会出现。这意味着当它稍后尝试查找此列表 var resultsList = form.find('.search-results'); 时,它没有找到它,因此无法填充项目。
我正在运行 Minimal 主题。 该网站是testing site,搜索栏位于顶部的灰色标题中,也在 /search 上
Shopify 为演示此自动完成功能而构建的测试站点位于 [https]://search-autocomplete.myshopify.com/。 <ul> 附加在页面加载时已经存在。
编辑:
做了更多的挖掘,我在开发工具中偶然发现了这个错误 -
未捕获的 ReferenceError: jQuery 未定义在 (index):7031
你猜对了,就是下面 jQuery 代码的第一行。 $(function() { 知道为什么 jQuery 是未定义的吗?该脚本包含在我的索引文件底部</body> 之前,因此 jquery.min.js 应该已经加载,站点上的其余 jQuery 工作正常。
测试网站上的表单代码
<form action="/search" method="get" class="site-header__search small--hide" role="search">
{% comment %}<input type="hidden" name="type" value="product">{% endcomment %}
<div class="site-header__search-inner">
<label for="SiteNavSearch" class="visually-hidden">{{ 'general.search.placeholder' | t }}</label>
<input type="search" name="q" id="SiteNavSearch" placeholder="{{ 'general.search.placeholder' | t }}" aria-label="{{ 'general.search.placeholder' | t }}" class="site-header__search-input">
</div>
<button type="submit" class="text-link site-header__link site-header__search-submit">
{% include 'icon-search' %}
<span class="icon__fallback-text">{{ 'general.search.submit' | t }}</span>
</button>
</form>
搜索.json
{% layout none %}
{% capture results %}
{% for item in search.results %}
{% assign product = item %}
{
"title" : {{ product.title | json }},
"url" : {{ product.url | within: product.collections.last | json }},
"thumbnail": {{ product.featured_image.src | product_img_url: 'thumb' | json }}
}
{% unless forloop.last %},{% endunless %}
{% endfor %}
{% endcapture %}
{
"results_count": {{ search.results_count }},
"results": [{{ results }}]
}
搜索-autocomplete.liquid
<script>
$(function() {
// Current Ajax request.
var currentAjaxRequest = null;
// Grabbing all search forms on the page, and adding a .search-results list to each.
var searchForms =
$('form[action="/search"]').css('position','relative').each(function() {
// Grabbing text input.
var input = $(this).find('input[name="q"]');
// Adding a list for showing search results.
var offSet = input.position().top + input.innerHeight();
$('<ul class="search-results"></ul>').css( { 'position': 'absolute', 'left': '0px', 'top': offSet } ).appendTo($(this)).hide();
// Listening to keyup and change on the text field within these search forms.
input.attr('autocomplete', 'off').bind('keyup change', function() {
// What's the search term?
var term = $(this).val();
// What's the search form?
var form = $(this).closest('form');
// What's the search URL?
var searchURL = '/search?type=product&q=' + term;
// What's the search results list?
var resultsList = form.find('.search-results');
// If that's a new term and it contains at least 3 characters.
if (term.length > 3 && term != $(this).attr('data-old-term')) {
// Saving old query.
$(this).attr('data-old-term', term);
// Killing any Ajax request that's currently being processed.
if (currentAjaxRequest != null) currentAjaxRequest.abort();
// Pulling results.
currentAjaxRequest = $.getJSON(searchURL + '&view=json', function(data) {
// Reset results.
resultsList.empty();
// If we have no results.
if(data.results_count == 0) {
// resultsList.html('<li><span class="title">No results.</span></li>');
// resultsList.fadeIn(200);
resultsList.hide();
} else {
// If we have results.
$.each(data.results, function(index, item) {
var link = $('<a></a>').attr('href', item.url);
link.append('<span class="thumbnail"><img src="' + item.thumbnail + '" /></span>');
link.append('<span class="title">' + item.title + '</span>');
link.wrap('<li></li>');
resultsList.append(link.parent());
});
// The Ajax request will return at the most 10 results.
// If there are more than 10, let's link to the search results page.
if(data.results_count > 10) {
resultsList.append('<li><span class="title"><a href="' + searchURL + '">See all results (' + data.results_count + ')</a></span></li>');
}
resultsList.fadeIn(200);
}
});
}
});
});
// Clicking outside makes the results disappear.
$('body').bind('click', function(){
$('.search-results').hide();
});
});
</script>
【问题讨论】: