简体   繁体   English

(jqueryUI)如何关闭自动完成功能?

[英](jqueryUI) How can I turn autocomplete off?

So I'm trying to recreate Facebooks tagging system in jquery. 因此,我试图在jquery中重新创建Facebooks标记系统。 I can not use any plugins. 我不能使用任何插件。 I have some of it already: 我已经有一些了:

<html lang="de">

<head>
    <title>Autocomplete</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>

    <script type="text/javascript">
      $(document).ready(function () {
          var triggered = false;
          var trigger = "@";
          var index = -1;

          $("input").autocomplete({
              source: [
                  "AAB",
                  "AAC",
                  "CCE",
                  "CCG",
                  "IIU",
                  "IIO"],
              search: function () {
                  if (!triggered) {
                      return false;
                  }
              },
              select: function (event, ui) {
                  var text = this.value;
                  var pos = text.lastIndexOf(trigger);

                  this.value = text.substring(0, pos + trigger.length) + ui.item.value;
                  triggered = false;
                  return false;
              },
              focus: function () {
                  return false;
              },
              minLength: 0
          }).on("keyup", function () {
              var text = this.value;
              var len = text.length;
              var last;
              var query;

              if (triggered) {
                  index = text.lastIndexOf(trigger);
                  query = text.substring(index + trigger.length);
                  $(this).autocomplete("search", query);

                  if (index === -1) {
                      triggered = false;
                  }
              } else if (len >= trigger.length) {
                  console.log("ELSE IF")
                  last = text.substring(len - trigger.length);
                  triggered = (last === trigger);
              }
          });
      });
    </script>
</head>

<body>
    <label>Input something here:</label>
    <br>
    <input type="text" value="" autofocus="true" />
</body>

</html>

So i got the following problem with it: Tagging works just fine, but after I hit the @ once the Autocomplete doesn't go away. 所以我遇到了以下问题:标记工作得很好,但是一旦我点击@之后,自动完成功能就不会消失。 It stops searching but it still displays that there were no results found. 它停止搜索,但仍显示未找到结果。 I tried to solve it as follows: 我试图解决如下:

  • I tried the "disabled" property inside the autocomplete which led to it not working at all. 我在自动完成功能中尝试了“禁用”属性,这导致该功能根本无法工作。
  • I tried the "destroy" method, but it led to me not being able to call the autocomplete after the first time 我尝试了“销毁”方法,但是这导致我第一次无法调用自动完成功能
  • I tried the "disable" methode which led to "there are 6 elements" (I'm paraphrasing here) being displayed 我尝试了“禁用”方法,该方法导致显示“有6个元素”(我在这里释义)
  • And finally I tried the close methode which does absolutly nothing. 最后,我尝试了绝对不做任何事情的close方法。

So my Question: Does anybody know a trick to how this could work? 所以我的问题是:有人知道如何发挥作用的窍门吗? do I have to rewrite my code somehow? 我是否必须以某种方式重写我的代码?

it is a little bit strange... but how about that in the keyup event: 这有点奇怪...但是在keyup事件中呢?

if(this.value == ""){
        triggered = false;
        $("input").autocomplete( "close" );
    }

See here 看这里

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

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