实现。访问自动完成元素属性

发布于 2025-01-27 12:51:36 字数 457 浏览 3 评论 0原文

我想答案应该很容易,但是我要生气地试图找到答案。

可以说,我有这样的物质化的自动完成元素。

 $('#element_id).autocomplete({
                limit:5,
                data:{"a":null,"b":null,"c":null},
                onAutocomplete:function(){whatEverYouWannaDoAfterCompletion()}
            });

但是,获得所选值没有问题,我的问题是... 是否可以使用ActiveIndex自动完成项目属性获得所选的索引,或者我应该“导航”通过渲染的组件来找到所选的术语?

谢谢。

I guess answer should be easy, but I´m going mad trying to find an answer.

Let´s say that I have a Materialize autocomplete element defined like this.

 $('#element_id).autocomplete({
                limit:5,
                data:{"a":null,"b":null,"c":null},
                onAutocomplete:function(){whatEverYouWannaDoAfterCompletion()}
            });

However, getting the selected value has no problem, my question is...
Is it possible to get selected index with activeIndex autocomplete item property or should I "navigate" throught rendered component to find the selected ocurrence?.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

装迷糊 2025-02-03 12:51:36

尽管最终目标对我来说尚不清楚,但我将尝试至少阐明属性 ActiveIndex 等等。

$(document).ready(function() {
  $('input').autocomplete({
    data: {
      a: null,
      b1: null,
      b2: null
    }
  });
  
  function checkIndex() {
    var el = $('input').get(0);
    var instance = M.Autocomplete.getInstance(el);
    console.log(instance.count, instance.activeIndex, $('input').val());
    setTimeout(checkIndex, 333);
  }
  
  checkIndex();
  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">

<div class="container input-field">
  <input class="autocomplete">
</div>

在这里,我准备了一个时间驱动的函数,以检查属性,而无需依赖元素焦点。关键步骤是将元素实例在物质空间(而不是jQuery!)中获取。

至于属性的理解,我认为我们应该在使用之前仔细测试它们。在我的测试中,即使选择完全匹配选项值,我也看到了唯一的activeIndex = -1值。预期的行为与我不同。

Although the final goal is not clear for me, I'll try to clarify at least how access the property activeIndex and some more.

$(document).ready(function() {
  $('input').autocomplete({
    data: {
      a: null,
      b1: null,
      b2: null
    }
  });
  
  function checkIndex() {
    var el = $('input').get(0);
    var instance = M.Autocomplete.getInstance(el);
    console.log(instance.count, instance.activeIndex, $('input').val());
    setTimeout(checkIndex, 333);
  }
  
  checkIndex();
  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">

<div class="container input-field">
  <input class="autocomplete">
</div>

Here I prepared a time-driven function to check properties with no dependency on the element focus. The key step is to get the element instance in Materialize space (not jQuery!).

As for the properties sense understanding, I think we should test them carefully before using. In my tests I have seen the only activeIndex=-1 value even if the selection match the option value entirely. The expected behavior is different to my mind.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文