最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Is there a way to make an HTML5 datalist use a fuzzy search? - Stack Overflow

matteradmin10PV0评论

I have a set of datalist options that I would like to fuzzy match when searching. For instance, if I type "PHP HTML" or "PHPAndHTML" I would like either of those to match the "PHP And HTML" option. Is there any way to do this? Please see this fiddle or the code below for an example.

<h1>Datalist Demo</h1>
<label for="default">Pick a programming language</label>
<input type="text" id="default" list="languages">
<datalist id="languages">
    <option value="HTML">
    <option value="CSS">
    <option value="JavaScript">
    <option value="Java">
    <option value="Ruby And Go">
    <option value="PHP And HTML">
    <option value="Go">
    <option value="Erlang">
    <option value="Python And C++">
    <option value="C">
    <option value="C#">
    <option value="C++">
</datalist>

I have a set of datalist options that I would like to fuzzy match when searching. For instance, if I type "PHP HTML" or "PHPAndHTML" I would like either of those to match the "PHP And HTML" option. Is there any way to do this? Please see this fiddle or the code below for an example.

<h1>Datalist Demo</h1>
<label for="default">Pick a programming language</label>
<input type="text" id="default" list="languages">
<datalist id="languages">
    <option value="HTML">
    <option value="CSS">
    <option value="JavaScript">
    <option value="Java">
    <option value="Ruby And Go">
    <option value="PHP And HTML">
    <option value="Go">
    <option value="Erlang">
    <option value="Python And C++">
    <option value="C">
    <option value="C#">
    <option value="C++">
</datalist>

I want to do this with a native datalist instead of a custom library. The reason being is that in my real-world scenario I have hundreds of inputs on my page that all use the same datalist, and with custom libraries it bees very CPU intensive, whereas if I tie all inputs to a single datalist it is actually very efficient.

Share Improve this question edited Aug 19, 2020 at 13:22 Alexandre Elshobokshy 10.9k6 gold badges34 silver badges60 bronze badges asked Dec 13, 2019 at 16:06 JakeJake 5071 gold badge6 silver badges17 bronze badges 3
  • What do you actually mean by 'fuzzy search'? A regular expression perhaps? – Poul Bak Commented Jun 28, 2020 at 20:43
  • @PoulBak A regex might work, I was hoping for an attribute switch that would make the browser more forgiving of spaces and maybe even spelling mistakes. Like the OP said: 'if I type "PHP HTML" or "PHPAndHTML" I would like either of those to match the "PHP And HTML" option.' Another way to achieve this (like with JS) would be fine too. – user4301448 Commented Jun 30, 2020 at 3:44
  • 1 I would use js; get the the values into js, and use fuse.js to search them. Its search capabilities are excellent – KayakinKoder Commented Jun 30, 2020 at 14:39
Add a ment  | 

1 Answer 1

Reset to default 8 +50

Datalist matching behavior is not customizable.

Even when you try and force the dropdown to be visible, it won't.

$(document).on('keyup', '#default', function() {
  let val = this.value.split(/[\s]+|(?=[A-Z][a-z])|(?<=[a-z])(?=[A-Z])/),
    reg = '';
  val.forEach(function(entry) {
    if (entry != '')
      reg += '.*' + entry + '.*'
  });

  var datalist = $('#languages option');
  $.each(datalist, function(i) {
    var option = datalist[i].value;
    if (!option.match(reg)) {
      $(datalist[i]).hide()
    } else {
      $(datalist[i]).show()
      $('#languages').show()
      console.log(datalist[i].value)
    }
  });
});
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.5.1/jquery.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
<h1>Datalist Demo</h1>
<label for="default">Pick a programming language</label>
<input type="text" id="default" list="languages">
<datalist id="languages" open="open">
    <option value="HTML">
    <option value="CSS">
    <option value="JavaScript">
    <option value="Java">
    <option value="Ruby And Go">
    <option value="PHP And HTML">
    <option value="Go">
    <option value="Erlang">
    <option value="Python And C++">
    <option value="C">
    <option value="C#">
    <option value="C++">
</datalist>

Feel free to test the code above, when you type, the right values are shown/hidden depending on the regex. But the datalist isn't being visible.

Another answer supposedly did something similar, but even that answer doesn't work, to prove my point. https://stackoverflow./questions/42592978/how-to-make-datalist-match-result-from-beginning-only

I would suggest relying on plugins for this particular use case if you want to have a customizable behavior.

Post a comment

comment list (0)

  1. No comments so far