2

我有一个包含很多(1000 多个)名称的 HTML 选择列表。我有一个 javascript,如果有人开始输入,它将选择第一个匹配的名称。此匹配查看项目的开头:

var optionsLength = dropdownlist.options.length;
  for (var n=0; n < optionsLength; n++)
  {
    var optionText = dropdownlist.options[n].text;
    if (optionText.indexOf(dropdownlist.keypressBuffer,0) == 0)
    {
      dropdownlist.selectedIndex = n;
      return false; 
    }
  }

客户希望有一个建议或自动过滤器:键入名称的一部分应该“找到”包含该部分的所有名称。我见过一些类似 Google Suggest 的选项,大多数都使用 Ajax,但我想要一个纯 JavaScript 选项,因为无论如何都已经加载了选择列表。有人指点吗?

4

4 回答 4

2

改变

if (optionText.indexOf(dropdownlist.keypressBuffer,0) == 0)

if (optionText.indexOf(dropdownlist.keypressBuffer) > 0)

在. dropdownlist.keypressBuffer_optionText

于 2008-10-09T08:23:09.197 回答
2

我会设置一个缓存来保存options我的select. 而不是在 中过滤optionsselect我会清除select,然后用 match 重新填充它options

大量伪代码:

onLoad:
    set cache

onKeyPress:
    clear select-element
    find option-elements in cache
    put found option-elements into select-element

这是我写的一个小 POC,selects从另一个中选择的内容中进行过滤select——实际上是将一堆选择链接在一起。

也许它可以给你一些想法:

function selectFilter(_maps)
{
    var map = {};


    var i = _maps.length + 1; while (i -= 1)
    {
        map = _maps[i - 1];


        (function (_selectOne, _selectTwo, _property)
        {
            var select = document.getElementById(_selectTwo);
            var options = select.options;
            var option = {};
            var cache = [];
            var output = [];


            var i = options.length + 1; while (i -= 1)
            {
                option = options[i - 1];

                cache.push({
                    text: option.text,
                    value: option.value,
                    property: option.getAttribute(_property)
                });
            }


            document.getElementById(_selectOne).onchange = function ()
            {
                var selectedProperty = this
                                       .options[this.selectedIndex]
                                       .getAttribute(_property);
                var cacheEntry = {};
                var cacheEntryProperty = undefined;


                output = [];

                var i = cache.length + 1; while (i -= 1)
                {
                    cacheEntry = cache[i - 1];

                    cacheEntryProperty = cacheEntry.property;

                    if (cacheEntryProperty === selectedProperty)
                    {
                        output.push("<option value=" + cacheEntry.value + " "
                        _property + "=" + cacheEntryProperty + ">" +
                        cacheEntry.text + "</option>");
                    }
                }

                select.innerHTML = output.join();
            };
        }(map.selectOne, map.selectTwo, map.property));
    }
}


$(function ()
{
    selectFilter([
        {selectOne: "select1", selectTwo: "select2", property: "entityid"},
        {selectOne: "select2", selectTwo: "select3", property: "value"}
    ]);
});
于 2008-10-09T09:40:41.070 回答
2

YUI 库有一个用于此类功能的库,称为AutoComplete

AutoComplete 的 DataSource 可以是本地 javascript 对象,如果您改变主意,也可以轻松切换到 Ajax。

YUI 组件是相当可定制的,具有相当多的功能。

编辑:我不确定您是否可以按照问题的要求使用选择框。也许是可能的。

于 2008-10-09T09:53:37.190 回答
1

使用这个过滤器脚本 http://www.barelyfitz.com/projects/filterlist/

于 2010-06-23T00:12:56.423 回答