【问题标题】:Materialize autocomplete drop-down not working实现自动完成下拉菜单不起作用
【发布时间】:2021-04-10 17:01:54
【问题描述】:

我一直在为我的项目使用实现自动完成功能,但在显示下拉菜单时遇到了问题。它只是无法出现!我读到具体化版本 0.98.2 可能会工作,我使用了那个 CDN,但它仍然无法工作。我怀疑触发下拉菜单存在一些问题。 (我可能是非常错误的)另一件事可能是错误的,因为我在某个地方搞砸了数据没有被使用。

我在获取和使用数据时没有问题,但下拉菜单不起作用。

html:

<div class="input-field">
    <label for="country">Autocomplete</label>
    <input type="text" id="company" class="autocomplete">
</div>

这是我的 js:

var company = document.getElementById("company");

  company.addEventListener("input", function () {
      $(function() {
      var inputValue = $('#company').val();
      var url = 'https://sandbox.iexapis.com/stable/search/'
              + encodeURIComponent(inputValue)
              + '/?token=****************';
      
       $.ajax({
        type: 'GET',
        url: url,
        success: function(response) {
          
          var companyArray = response;
          var dataCompany = {};
          for (var i = 0; i < companyArray.length; i++) {
            //console.log(countryArray[i].name);
            console.log(url);
            dataCompany[companyArray[i].securityName] = companyArray[i].symbol;
            console.log(companyArray[i].securityName);
            console.log(companyArray[i].symbol);
          }
          $('input.autocomplete').autocomplete({
            data: dataCompany
          });
        }
      });
    });
  });

由于有人要求回复,我在这里放了一些屏幕截图! 这是我输入“wa”的时候

问题不在于数据不存在,而在于下拉选择框没有出现。

【问题讨论】:

  • 你好,你能显示response的输出吗?
  • @Swati umm,我可以给你看控制台日志,但是我没有太多可以给你看的东西,因为下拉菜单没有出现,所以我不能显示它没有出现但是我会尽力添加一些信息截图,另外,确实没有错误消息,它只是不起作用。

标签: javascript ajax materialize


【解决方案1】:

您可以首先初始化您的自动完成并将其实例存储在某个变量中。然后,每当用户在输入框中键入时,只需使用该实例来更新自动完成中的数据,即:instance.updateData(dataCompany)

演示代码

//suppose this is response return from ajax
var response = [{
  "securityName": "abc",
  "symbol": "scsc"
}, {
  "securityName": "abc2",
  "symbol": "scsc2"
}]

$(function() {
  $('input#company').autocomplete() //intialize your autocomplete
  let instance = M.Autocomplete.getInstance($('input#company')); //get instance
  $('input.autocomplete').on("input", function() {
    var inputValue = $('#company').val();
    /*var url = 'https://sandbox.iexapis.com/stable/search/' +
      encodeURIComponent(inputValue) +
      '/?token=****************';
    $.ajax({
      type: 'GET',
      url: url,
      success: function(response) {*/
    var companyArray = response;
    var dataCompany = {};
    for (var i = 0; i < companyArray.length; i++) {
      dataCompany[companyArray[i].securityName] = companyArray[i].symbol;

    }
    //updatedata
    instance.updateData(dataCompany)
    /*  }
    });*/

  });
  })
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>


<div class="input-field">
  <label for="country">Autocomplete</label>
  <input type="text" id="company" class="autocomplete" autocomplete="off">
</div>

【讨论】:

  • 这似乎很有希望!我会试试这个,我认为这会奏效!感谢您的宝贵时间!
  • 它给出了以下错误,我可能做错了什么?未捕获的类型错误:无法读取未定义的属性“updateData”
  • 我将添加一个codepen链接,请看一下我的代码,我可能做错了什么,我对后端工作的JS没有太多经验。 codepen.io/VardhanMahajan/pen/poRLvYO
  • 是的,请使用我在答案中添加的 js 代码,看看是否可行..
  • 感谢它的工作!但我收到很多错误imgur.com/zHpumrK
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多