我正在接受用户的输入,然后显示匹配的结果。因此,如果用户键入“a”,则会出现以下文本: Football Baseball Fast 如果用户在 a ('as') 后键入 s。显示以下文本 Football Baseball Fast Baseball Fast。但是当用户键入 as 时,我想删除前 3 个结果。
// 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 on onkeyup()
{
s1 = document.getElementById('filter').value;
var myRegex = new RegExp((s1),"ig");
arraysearch(myRegex);
}
function arraysearch(myRegex)
{
for(i=0; i<myArray.length; i++)
{
if (myArray[i].match(myRegex))
{
document.getElementById('placeholder').innerHTML += myArray[i] + "<br/>";
}
}
}