Javascript做模糊查询

时间:2023-01-07 10:31:20

<html>
<head>
<title>Javascript模糊查找</title>
</head>
<body>

<li onload="load('Name')" id="name">Name</li>
<li onload="load('sex')" id="sex">sex</li>
<li onload="load('age')" id="age">age</li>
<li onload="load('job')" id="job">job</li>
<li onload="load('mail')" id="mail">E-mail</li>

<input id="input" type="text" value="" />
<input id="search" type="button" onclick="findEach()" value="Search" />

<script>
var vData= ["name", "sex", "age", "job", "E-mail"];

function load(id)
{
alert(vData[0]);
//vData[vData.length] = document.getElementById(id).innerHTML;
}

function find(sFind, sObj)
{
var nSize = sFind.length;
var nLen = sObj.length;

var sCompare;

if(nSize <= nLen ){
for(var i = 0; i <= nLen - nSize; i++){
sCompare = sObj.toString().substring(i, i + nSize);
if(sCompare == sFind){
return i;
}
}
}

return -1;
}

function findEach()
{
var sFind = document.getElementById("input").value;
if(sFind==""){
alert("Can not be empty");
}

if(sFind!=""){
var nPos;
var vResult = [];

for(var i in vData){
var sTxt=vData[i]||'';
nPos=sTxt.indexOf(sFind);
if(nPos>=0){
vResult[vResult.length] = sTxt;
}
}

alert(vResult);
}
}
</script>
</body>
</html>

转载地址:http://blog.csdn.net/yorhomwang/article/details/7888597