好吧,我创建了一个简单的搜索器,但是当单词不存在时我需要返回一条消息,但是第一个单词可以正常工作,但是当我写最后一个单词时,循环立即中断,如何浏览所有单词并发送一个警报而不打破循环?
提前致谢
<html>
<head>
<title>buscador</title>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<script type="text/javascript">
$(function(){
/*iniciando el buscador*/
$("#buscar").click(function(){
var dato =$("#palabra").val().toLowerCase();
var myArray = [ "hello", "world","apple","bread","milk","pencil"];
for ( var i = 0; i < myArray.length; i = i + 1 ){
var flag = false;
if(dato==myArray[i])
{
alert("exist");
return false;
}
else if(dato!=myArray[i]){
flag=true;
}
if(flag) {alert("no exist"); return false;}
}/*end foreach*/
});
});
</script>
<body>
<form method="post" action="" id="form-search">
<input type="text" id="palabra" name="palabra" placeholder="buscar"/>
<button id="buscar" name="buscar">Buscar</button>
</form>
</body>
</html>