admin管理员组

文章数量:1277899

Someone helped me with this earlier, but there is one problem and I already closed that question. I do not want to use JQuery. The following code works, it allows you to search a dropdown menu:

<html>
  <head>
    <script type="text/javascript">
    function searchSel() 
    {
      var input = document.getElementById('realtxt').value.toLowerCase(),
          len = input.length,
          output = document.getElementById('realitems').options;
      for(var i=0; i<output.length; i++)
        if (output[i].text.toLowerCase().slice(0, len) == input)
          output[i].selected = true;
      if (input == '')
        output[0].selected = true;
    }
    </script>
  </head>
  <body>
    <form>
      search <input type="text" id="realtxt" onkeyup="searchSel()">
      <select id="realitems">
        <option value="">select...</option>
        <option value="1">Power hungry</option>
        <option value="2">Super man</option>
        <option value="3">Hyperactive</option>
        <option value="4">Bored</option>
        <option value="5">Human</option>
      </select>
    </form>
  </body>
</html>

Here is the problem.

It is only limited to the matching first letters of the first word. So if you typed in Super for Super Man, it would work. But if you typing with Man it will not show. Is there a way to make it match the whole string for a matching value? Thanks.

Someone helped me with this earlier, but there is one problem and I already closed that question. I do not want to use JQuery. The following code works, it allows you to search a dropdown menu:

<html>
  <head>
    <script type="text/javascript">
    function searchSel() 
    {
      var input = document.getElementById('realtxt').value.toLowerCase(),
          len = input.length,
          output = document.getElementById('realitems').options;
      for(var i=0; i<output.length; i++)
        if (output[i].text.toLowerCase().slice(0, len) == input)
          output[i].selected = true;
      if (input == '')
        output[0].selected = true;
    }
    </script>
  </head>
  <body>
    <form>
      search <input type="text" id="realtxt" onkeyup="searchSel()">
      <select id="realitems">
        <option value="">select...</option>
        <option value="1">Power hungry</option>
        <option value="2">Super man</option>
        <option value="3">Hyperactive</option>
        <option value="4">Bored</option>
        <option value="5">Human</option>
      </select>
    </form>
  </body>
</html>

Here is the problem.

It is only limited to the matching first letters of the first word. So if you typed in Super for Super Man, it would work. But if you typing with Man it will not show. Is there a way to make it match the whole string for a matching value? Thanks.

Share Improve this question edited Jan 13, 2014 at 7:23 Dhanu Gurung 8,86011 gold badges49 silver badges60 bronze badges asked Jan 13, 2014 at 7:20 user3187352user3187352 471 gold badge1 silver badge6 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

I have created a fiddle please check. http://jsfiddle/wLzs4/3/

I have used the indexOf javascript function for your case.

function searchSel() 
    {
      var input = document.getElementById('realtxt').value.toLowerCase();

          len = input.length;
          output = document.getElementById('realitems').options;
      for(var i=0; i<output.length; i++)
          if (output[i].text.toLowerCase().indexOf(input) != -1 ){
          output[i].selected = true;
              break;
          }
      if (input == '')
        output[0].selected = true;
    }

本文标签: javascriptWorking code to search within dropdown menuStack Overflow