你错过了几件事。
首先,您需要解析您的
b
字符串,以创建字符串数组 ["Apple", "Banana"]
。innerHTML()
函数,但您不能这样做。for
循环。
function test(){
var a = document.getElementById('search').value;
var b = "Apple, Banana";
var ar = b.split(", "); // arrray of strings ["Apple", "Banana"]
for (var c = 0; c <= 1; c++) {
if (a.match(ar[c])){ // no innerHTML function
document.getElementById('show').innerHTML = "You search " + "'" + ar[c] + "'";
return; // return after match
} else
document.getElementById('show').innerHTML = "ERROR.";
}
}
<i id="show"></i>//Output will show here//
<input id="search" type="text" placeholder="Search...">//Search Bar//
<button value="submit" onclick="return test()" type="submit">Search</button>