【问题标题】:Need to do multiple search at a time with starting character which is search in autocomplete需要使用自动完成搜索的起始字符一次进行多个搜索
【发布时间】:2015-09-29 16:22:22
【问题描述】:

我在我的网站中使用https://jqueryui.com/autocomplete/#multiple 来实现自动完成功能。它工作正常,现在我想搜索仅添加到文本框中的第一个字符的记录。

这是我的代码:

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css"><input type="text" id="multi"><script>$(document).ready(function() {var avail = [
    "ActionScript",
    "AppleScript",
    "Asp",
    "BASIC",
    "C",
    "C++",
];

$("#single").autocomplete({
    source:avail
});


 function split( val ) {
  return val.split( /,\s*/ );
}
function extractLast( term ) {
  return split( term ).pop();
}

$( "#multi" )
  // don't navigate away from the field on tab when selecting an item
  .bind( "keydown", function( event ) {
    if ( event.keyCode === $.ui.keyCode.TAB &&
        $( this ).autocomplete( "instance" ).menu.active ) {
      event.preventDefault();
    }
  })
  .autocomplete({
    minLength: 0,
    source: function( request, response ) {
      // delegate back to autocomplete, but extract the last term
      response( $.ui.autocomplete.filter(
        avail, extractLast( request.term ) ) );
    },
    focus: function() {
      // prevent value inserted on focus
      return false;
    },
    select: function( event, ui ) {
      var terms = split( this.value );
      // remove the current input
      terms.pop();
      // add the selected item
      terms.push( ui.item.value );
      // add placeholder to get the comma-and-space at the end
      terms.push( "" );
      this.value = terms.join( ", " );
      return false;
    }
  });});</script>

【问题讨论】:

标签: javascript jquery-ui-autocomplete


【解决方案1】:

根据 cmets:

要使typeahead.js multipel search demo 搜索“从第一个字符开始”,请更改匹配代码行:

~item.toLowerCase().indexOf(tquery.toLowerCase())

到这里:

item.toLowerCase().indexOf(tquery.toLowerCase()) == 0

演示小提琴:http://jsfiddle.net/BwDmM/1100/

附加说明:关于原始匹配代码如何工作的更多解释(tilda 位运算符)可以在这里找到:http://www.joezimjs.com/javascript/great-mystery-of-the-tilde/

【讨论】:

  • 对不起ivan,请参考我上面的问题,我刚刚把我的代码放在那里。
  • 我也找到了一个答案,stackoverflow.com/questions/19177541/… 但有点困惑,如何在我的代码中使用它。供您参考
  • 问题是关于 typeahead.js(我在我的网站中使用 typeahead.js 来实现自动完成功能。它工作正常),但您发布的代码仅使用 jQuery .我在这里错过了什么?
  • 非常抱歉,请忘记 typeahead.js 并考虑以上代码进行多次搜索。
  • 实际上我可能无法使用 typeahead.js 所以我现在使用这个,jqueryui.com/autocomplete/#multiple
猜你喜欢
  • 2015-01-22
  • 2012-01-14
  • 2015-04-10
  • 2012-06-03
  • 1970-01-01
  • 2019-06-28
  • 2012-07-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多