简体   繁体   English

为什么自动完成在这个带有 Bootstrap 的示例中不起作用?

[英]Why auto complete does not work in this example with Bootstrap?

I am trying to implement an auto complete dropdown with dynamic data but it doesnt display any suggestions in the dropdown.我正在尝试使用动态数据实现自动完成下拉列表,但它没有在下拉列表中显示任何建议。 I am using this example - Datalists: https://getbootstrap.com/docs/5.1/forms/form-control/ which works fine with predefined option tags.我正在使用这个示例 - 数据列表: https://getbootstrap.com/docs/5.1/forms/form-control/与预定义的选项标签一起工作。

<label for="exampleDataList" class="form-label">Datalist example</label>
   <input class="form-control" list="datalistOptions" id="exampleDataList" placeholder="Type to search...">
   <datalist id="datalistOptions">
       ... dynamic data here
   </datalist>

I do receive data from the PHP script and they are correctly accessed but I think the problem might be due to the delay of the fetch.我确实从 PHP 脚本接收到数据,并且它们被正确访问,但我认为问题可能是由于提取延迟。 HTML might expect the data to already be there when loaded. HTML 可能期望数据在加载时已经存在。 That's why maybe it works with existing data.这就是为什么它可能适用于现有数据的原因。

here is the javacsript code inside the fetch function where I dynamically produce the tags:这是 fetch function 中的 javacsript 代码,我在其中动态生成标签:

 var select = document.getElementById("datalistOptions");
 select.innerHTML = "";

 for (var key in data['result']) {
    var val = data['result'][key];
    if (data['result'].hasOwnProperty(key) && key != "error") {
       var val = data['result'][key];
      
       if (val != "") {
          var option = document.createElement("option");              
          option.value = val.id;
          select.appendChild(option);
       }
    }
 }

Update: I changed the js code a bit.更新:我稍微更改了 js 代码。 Now it uses appendChild instead of add function.现在它使用 appendChild 而不是添加 function。 The previous one was not adding any options to the datalist.前一个没有向数据列表添加任何选项。 appendChild does add options to the list but it does not display them. appendChild 确实将选项添加到列表中,但它不显示它们。

This is a simpler version of what you trying to do.这是您尝试做的更简单的版本。 Try to take it, add your conditions, and make that data come in that form if you can.试着接受它,添加你的条件,如果可以的话,让数据以那种形式出现。

     var data = ["Haifa","Tel Aviv","Jerusalem"];
        var select = document.getElementById("datalistOptions");
        select.innerHTML = "";
        for (var key in data) {
                var option = document.createElement("option");
                option.label = data[key];
                option.value = data[key];
                select.appendChild(option);
          }

And make sure you adds onclick event that calls you script, maybe this is you problem, that you not calling the script - I would recomand somethig like that:并确保添加调用脚本的 onclick 事件,也许这是你的问题,你没有调用脚本 - 我会推荐这样的东西:

<input onclick="datalistCreate()" class="form-control" list="datalistOptions" id="exampleDataList" placeholder="Type to search...">

datalistCreate() is the function name in the script. datalistCreate() 是脚本中的 function 名称。

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

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