简体   繁体   English

有没有办法显示与用户输入不匹配的数据列表选项?

[英]Is there a way to show datalist options that don't match user input?

I've got two elements, a datalist and an input :我有两个元素,一个datalist和一个input

<datalist id="choices"></datalist>

<input id="custom_id" name="custom_name" type="text" value="" list="choices" class="medium-field"/>

Additionally,here is an AJAX request, which populates the datalist (this works; not my issue)此外,这里有一个AJAX请求,它填充数据列表(这有效;不是我的问题)

var params = {
    "partial": $("#custom_id").value;
};
$.ajax({
      cache: true,
      url: "http://www.example.com",
      type: "POST",
      data: JSON.stringify(params),  // contains the input.value
      contentType: "application/json",
      headers: {
        "Range": "0-9"
      },
      dataType: "json",
      processData: false,
      // On success, create <option> elements and append them to datalist
      success: function(data) { .. };

The data returned by the REST endpoint is formatted like so: REST 端点返回的数据格式如下:

[
  { 
    "label": "active",
    "name": "iron"
  }, 
  ..
]

Basically the AJAX request hits a REST endpoint in front of a PostgreSQL db that does runs a SELECT based on the provided partial parameter, and returns a 2-column table (which gets formatted as the above response).基本上, AJAX请求命中 PostgreSQL 数据库前面的 REST 端点,该 PostgreSQL 数据库根据提供的partial参数运行SELECT ,并返回一个 2 列表(其格式为上述响应)。

However, I have situations where the data returned from the AJAX request is spelled differently from the user's input;但是,我遇到了从AJAX请求返回的数据与用户输入的拼写不同的情况; for example, when input.value = 'magnet' , I sometimes return a list of options where a few might read 'iron' .例如,当input.value = 'magnet' ,我有时会返回一个选项列表,其中一些可能会读取'iron'

The problem : due to 'iron' being spelled differently than 'magnet' , the user won't see this option in the datalist dropdown (even though the option element is created) unless the user actually types 'iron' .问题:由于'iron'的拼写与'magnet'不同,除非用户实际键入'iron'否则用户不会在数据列表下拉列表中看到此选项(即使创建了 option 元素)。 Is there a way for me to display 'iron' in the datalist even though it doesn't match what the user has typed?有没有办法让我在数据列表中显示'iron' ,即使它与用户输入的内容不匹配?

As far as I can understand your question, you are willing to show the synonyms as a result when an input is typed in the search box, ie if a user types magnet , he shall see the synonyms to 'magnet', which in this case happens to be iron What you can do is when the ajax hit is sent to example.com I understand that there is some SQL code as SELECT keyword FROM table_name WHERE keyword LIKE '%<whatever string is in data>%' .据我了解您的问题,您愿意在搜索框中输入输入时显示同义词,即如果用户输入magnet ,他将看到“magnet”的同义词,在这种情况下碰巧是iron你可以做的是当 ajax 命中发送到 example.com 我知道有一些 SQL 代码作为SELECT keyword FROM table_name WHERE keyword LIKE '%<whatever string is in data>%' This would return 'magnet' even if the user types mag.即使用户输入 mag,这也会返回 'magnet'。 Now coming to returning 'iron' and 'magnet' when a user types say 'mag' You will have to modify the table a little bit as follows: 1. Create a keywords table having columns: id, keyword and synonym_to_keyword 2. Enter the data like (1, 'iron' , null) (2,'magnet',1)现在要在用户键入 say 'mag' 时返回 'iron' 和 'magnet' 您必须对表进行如下修改: 1. 创建一个包含以下列的关键字表:id、关键字和同义词_to_keyword 2. 输入数据如(1, 'iron' , null) (2,'magnet',1)

  1. Now modify the query as: SELECT id as I, keyword FROM table_name WHERE keyword like '%<whatever string is in data>%' OR keyword like (SELECT keyword FROM table_name WHERE synonym_to_keyword = I)现在将查询修改为: SELECT id as I, keyword FROM table_name WHERE keyword like '%<whatever string is in data>%' OR keyword like (SELECT keyword FROM table_name WHERE synonym_to_keyword = I)

Note that you will have to maintain a list of keywords and probable synonyms to the keywords so that the user sees all the probable options (like magnet, iron, etc) when he types 'magnet'.请注意,您必须维护一个关键字列表和关键字的可能同义词,以便用户在键入“磁铁”时看到所有可能的选项(如磁铁、铁等)。

Since Chrome will compare both the value and label to see if it should show the datalist option, you can set all of your option labels to the value you have in your input for them to show up.由于 Chrome 会比较值和标签以查看它是否应该显示 datalist 选项,因此您可以将所有选项标签设置为输入中的值,以便它们显示。

Relevant Chromium patch 相关 Chromium 补丁

Code Example below:下面的代码示例:

 function autoSuggestAddress(input) { if (input.value) { // dummy data let suggestions = ["Example 1", "Example 2", "Example 3"]; // clear old datalist let datalist = input.list; while (datalist.hasChildNodes()) { datalist.removeChild(datalist.firstChild); } suggestions.forEach((item) => { let option = document.createElement("option"); option.label = input.value; option.value = item; datalist.appendChild(option); }); } }
 <form method="GET" id="search-form"> <label for="address">Address</label> <input type="text" id="address" name="address" form="search-form" oninput="autoSuggestAddress(this)" type="search" value="" required autocomplete="off" list="suggestions" /> <datalist id="suggestions"> </datalist> </form>

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

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