JS实现自动匹配搜索字符
2010-11-24 14:12
267 查看
JS如下:
var TextUtil = new Object();
var ListUtil = new Object();
ListUtil.remove = function(oListbox,iIndex)
{
oListbox.remove(iIndex);
}
ListUtil.clear = function(oListbox)
{
for(var i = oListbox.options.length -1; i >=0;i--)
{
ListUtil.remove(oListbox,i);
}
}
ListUtil.add = function(oListbox,sName,sValue)
{
var oOption = document.createElement("option");
oOption.appendChild(document.createTextNode(sName));
if(arguments.length == 3)
{
oOption.setAttribute("value",sValue);
}
oListbox.appendChild(oOption);
}
TextUtil.autosuggestMatch= function (sText,arrValues)
{
var arrResult = new Array;
if(sText !="")
{
for(var i = 0; i < arrValues.length;i++)
{
if(arrValues[i].indexOf(sText) == 0)
{
arrResult.push(arrValues[i]);
}
}
}
return arrResult;
}
TextUtil.autosuggest = function(oTextbox,arrValues,sListboxId)
{
var oListbox = document.getElementById(sListboxId);
ListUtil.clear(oListbox);
var arrMatches = TextUtil.autosuggestMatch(oTextbox.value,arrValues);
for(var i = 0; i < arrMatches.length;i++)
{
ListUtil.add(oListbox,arrMatches[i]);
}
}
HTML如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title></title>
<script src=JScript1.js type="text/javascript"></script>
<script type="text/javascript" >
var arrColors = ["red","orange","yellow","green","blue","indigo","violet","brown","black","tan","ovory","navy",
"aqua","white","purple","pink","gray","silver"];
arrColors.sort();
function setText(oListbox,sTextboxId)
{
var oTextbox = document.getElementById(sTextboxId);
if(oListbox.selectedIndex>-1)
{
oTextbox.value = oListbox.options[oListbox.selectedIndex].text;
}
}
</script>
</head>
<body>
<input type ="text" value = "" id="txtColor" onkeyup="TextUtil.autosuggest(this,arrColors,'lstColors')" /><br />
<select id="lstColors" size="5" style="width:200px" onclick="setText(this,'txtColor')"></select>
</body>
</html>
var TextUtil = new Object();
var ListUtil = new Object();
ListUtil.remove = function(oListbox,iIndex)
{
oListbox.remove(iIndex);
}
ListUtil.clear = function(oListbox)
{
for(var i = oListbox.options.length -1; i >=0;i--)
{
ListUtil.remove(oListbox,i);
}
}
ListUtil.add = function(oListbox,sName,sValue)
{
var oOption = document.createElement("option");
oOption.appendChild(document.createTextNode(sName));
if(arguments.length == 3)
{
oOption.setAttribute("value",sValue);
}
oListbox.appendChild(oOption);
}
TextUtil.autosuggestMatch= function (sText,arrValues)
{
var arrResult = new Array;
if(sText !="")
{
for(var i = 0; i < arrValues.length;i++)
{
if(arrValues[i].indexOf(sText) == 0)
{
arrResult.push(arrValues[i]);
}
}
}
return arrResult;
}
TextUtil.autosuggest = function(oTextbox,arrValues,sListboxId)
{
var oListbox = document.getElementById(sListboxId);
ListUtil.clear(oListbox);
var arrMatches = TextUtil.autosuggestMatch(oTextbox.value,arrValues);
for(var i = 0; i < arrMatches.length;i++)
{
ListUtil.add(oListbox,arrMatches[i]);
}
}
HTML如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title></title>
<script src=JScript1.js type="text/javascript"></script>
<script type="text/javascript" >
var arrColors = ["red","orange","yellow","green","blue","indigo","violet","brown","black","tan","ovory","navy",
"aqua","white","purple","pink","gray","silver"];
arrColors.sort();
function setText(oListbox,sTextboxId)
{
var oTextbox = document.getElementById(sTextboxId);
if(oListbox.selectedIndex>-1)
{
oTextbox.value = oListbox.options[oListbox.selectedIndex].text;
}
}
</script>
</head>
<body>
<input type ="text" value = "" id="txtColor" onkeyup="TextUtil.autosuggest(this,arrColors,'lstColors')" /><br />
<select id="lstColors" size="5" style="width:200px" onclick="setText(this,'txtColor')"></select>
</body>
</html>
相关文章推荐
- 利用js 进行输入框自动匹配字符的小例子
- JQuery+ajax实现类似百度搜索自动匹配功能
- js实现连续英文字符自动换行
- JS仿百度搜索自动提示框匹配查询功能
- jsp实现的数据库模糊搜索(可以自己设定匹配字符个数)
- js正则实现前端搜索,匹配字符加红
- js及ajax实现搜索下拉自动提示
- JS实现仿百度输入框自动匹配功能的示例代码
- 利用PHP+JS实现搜索自动提示(实例)
- jquery 实现类似于百度自动匹配搜索
- JS仿百度搜索自动提示框匹配查询功能
- 利用PHP+JS实现搜索自动提示(实例)
- JQuery+ajax实现类似百度搜索自动匹配功能
- JS判断来路是否是百度等搜索索引进行弹窗或自动跳转的实现代码
- [转] 利用AJAX实现搜索提示功能 (关键字自动匹配功能)
- C#实现Combobox自动匹配字符
- 利用js进行输入框自动匹配字符
- WPF DevExpress ComboBoxEdit 实现键值绑定,输入字符自动匹配
- jsp实现的数据库模糊搜索(可以自己设定匹配字符个数)