我正在尝试显示与用户输入匹配的文本。它在用户输入第一个字符时显示结果,例如“a”,但当输入第二个字符时没有任何反应,例如“as”。所以我希望当用户输入时显示棒球和快速。
// JavaScript Document
s1= new String()
s2= new String()
var myArray = new Array();
myArray[0] = "Football";
myArray[1] = "Baseball";
myArray[2] = "Cricket";
myArray[3] = "Fast";
function test() // called onkeyup() event
{
s1 = document.getElementById('filter').value;
s2 = s2+s1;
arraysearch();
}
function arraysearch()
{
for(i=0; i<myArray.length; i++)
{
if (myArray[i].indexOf(s2) != -1) // also tried using match method
{
alert('Matched');
document.getElementById('placeholder').innerHTML += myArray[i] + "<br/>";
}
}
}