js模糊查询

<!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>

 

posted @ 2017-05-27 00:14  lyg0126  阅读(1548)  评论(0编辑  收藏  举报