js模糊查询
Posted lyg0126
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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>
以上是关于js模糊查询的主要内容,如果未能解决你的问题,请参考以下文章