Javascript fuzzy search

Source: Internet
Author: User

<Li> on the page, when you enter any letter, press the button next to it to find the words that contain your input. <li> to test the program performance, you can change the elements in the array (the search is performed on the array, so the words in <li> are irrelevant to the search .)

<Html>

<Head>
<Title> Javascript fuzzy search </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. 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 = 0; I <= vData. length; I ++ ){
For (var I in vData ){
// NPos = find (sFind, vData [I]);
Var sTxt = vData [I] | '';
NPos = sTxt. indexOf (sFind );
If (nPos> = 0 ){
VResult [vResult. length] = sTxt;
}
}


Alert (vResult );
}
}


</Script>
</Body>
</Html>


Author: yorhomwang

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.