js模糊查询
直接上代码,其实关键的思路:就是字符比较的次数(这个地方我不是很明白),望大家指教
本文的代码转自:http://www.cnblogs.com/ducle/archive/2012/08/29/2661880.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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function findEach()
{
var vData= ["name", "sex", "age", "job", "e-mail"]; //查询字符集
var sFind = document.getElementById("input").value;
if(sFind==""){
alert("Can not be empty.");
}
if(sFind!=""){
var nPos;
var vResult = [];
//for(var i = 0; i <= vData.length; i++){
for(var i in vData){
var sTxt=vData[i]||'';
nPos = find(sFind, sTxt);
//nPos=sTxt.indexOf(sFind);
if(nPos>=0){
vResult[vResult.length] = sTxt;
}
}
alert(vResult);
}
}
function find(sFind, sObj)
{
var nSize = sFind.length;
var nLen = sObj.length;
var sCompare;
if(nSize <= nLen ){
for(var i = 0; i <= nLen - nSize + 1; i++){
sCompare = sObj.substring(i, i + nSize);
if(sCompare == sFind){
return i;
}
}
}
return -1;
}
</script>
</head>
<body>
<input id="input" type="text" value="" />
<input id="search" type="button" onclick="findEach()" value="Search" />
</body>
</html>