JavaScript autocomplete without external library

后端 未结 6 2094
面向向阳花
面向向阳花 2020-12-23 11:33

Is there a javascript autocomplete library that does not depend on any other libraries?

I am not using jQuery or the likes as I am making a mobile app that I need to

6条回答
  •  情深已故
    2020-12-23 12:03

    Here is a basic JavaScript example, which could be modified into an autocomplete control:

    var people = ['Steven', 'Sean', 'Stefan', 'Sam', 'Nathan'];
    
    function matchPeople(input) {
      var reg = new RegExp(input.split('').join('\\w*').replace(/\W/, ""), 'i');
      return people.filter(function(person) {
        if (person.match(reg)) {
          return person;
        }
      });
    }
    
    function changeInput(val) {
      var autoCompleteResult = matchPeople(val);
      document.getElementById("result").innerHTML = autoCompleteResult;
    }
    
    

提交回复
热议问题