码迷,mamicode.com
首页 > Web开发 > 详细

js模糊查询

时间:2017-05-27 10:51:30      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:put   字符   标题   1.0   xhtml 1.0   ret   ack   size   click   

<!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模糊查询

标签:put   字符   标题   1.0   xhtml 1.0   ret   ack   size   click   

原文地址:http://www.cnblogs.com/lyg0126/p/6911050.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!