jQuery自动完成输入密钥angolia

时间:2018-04-13 08:12:46

标签: javascript jquery autocomplete algolia

当前在所选元素上按 Enter 键时,自动完成操作是将元素值放入输入框。

如何修改行为,以便按元素上的 Enter 键会触发嵌入url的元素。为了简化,我想使 Enter 键与返回元素上的鼠标点击具有相同的行为。

由于

$('#search_input').autocomplete(
        { 
            //autoselect: true,
            //autoselectOnBlur: true,
            hint : false,
            templates: {
                dropdownMenu: '#my-custom-menu-template',
            }
        }, 
        [
            {
              source: $.fn.autocomplete.sources.hits(indexProducts, { hitsPerPage: 5 }),
              name : 'products',
              displayKey: 'name',
              templates: {
                suggestion: function(suggestion) {                    
                  return '<a href="'+suggestion.url+'">'+suggestion._highlightResult.name.value+'</a>';
                }
              }
            },
            {
              source: $.fn.autocomplete.sources.hits(indexCategories, { hitsPerPage: 5 }),
              name : 'categories',
              displayKey: 'name',
              templates: {
                suggestion: function(suggestion) {
                  return '<a href="'+suggestion.url+'">'+suggestion._highlightResult.name.value+' en <b>' + suggestion.parent_name+'</b></a>';
                }
              }
            }
        ]).on('autocomplete:selected', function(event, suggestion, dataset) {
    console.log(suggestion, dataset);
  });

1 个答案:

答案 0 :(得分:1)

解决方案添加自动填充功能:所选事件:

$('#search_input').autocomplete(
        { 
            //autoselect: true,
            //autoselectOnBlur: true,
            hint : false,
            templates: {
                dropdownMenu: '#my-custom-menu-template',
            }
        }, 
        [
            {
              source: $.fn.autocomplete.sources.hits(indexProducts, { hitsPerPage: 5 }),
              name : 'products',
              displayKey: 'name',
              templates: {
                suggestion: function(suggestion) {                    
                  return '<a href="'+suggestion.url+'">'+suggestion._highlightResult.name.value+'</a>';
                }
              }
            },
            {
              source: $.fn.autocomplete.sources.hits(indexCategories, { hitsPerPage: 5 }),
              name : 'categories',
              displayKey: 'name',
              templates: {
                suggestion: function(suggestion) {
                  return '<a href="'+suggestion.url+'">'+suggestion._highlightResult.name.value+' en <b>' + suggestion.parent_name+'</b></a>';
                }
              }
            }
        ]).on('autocomplete:selected', function(event, suggestion, dataset) {
                location.href = suggestion.url;
            });