gpt4 book ai didi

javascript - 一旦在数组中找到匹配项,如何停止JavaScript循环

转载 作者:行者123 更新时间:2023-12-02 05:48:52 25 4
gpt4 key购买 nike

我尝试找到一些修复程序,但似乎没有找到我想要的东西。也许是我的措辞。

所以我有这种形式,一旦它提交了一个5位数的邮政编码,它就会检查数组中是否有匹配的值,一旦匹配,我希望它显示成功消息,如果不匹配,则返回No match,但是当前返回返回消息与数组一样多的时间。我对JavaScript还是很陌生,因此将不胜感激。提前致谢!

JavaScript:

<script>
function getZip(e) {
e.preventDefault();
var zip = document.getElementById('zip').value; // Gets the value from the text field "zip" on the form.
var result = ["48650", "48611", "48746", "48614", "48706"];

for (var i = 0; i < result.length; i++) {
//console.log(result[i]);

//if (zip.length == 5 && zip == result[i]){
if (zip == result[i]) {
console.log(zip + " is = to " + result[i]);
alert('Match ' + zip);

} else if (result[i] != zip) {
alert("No Match1");
console.log(zip + " is != to " + result[i]);

} else {
alert("No Match2");
console.log(zip + " is != to " + result[i]);
}
//return false;
}
if (zip === "") { // Checks if the text field "zip" is empty, if so it returns an error message.
alert('Please enter a zip code!');

} else if (zip.length > 5) {
alert("Please enter a valid 5 digit numeric zip code.");

} else if (zip.length < 5) {
alert("Please enter a valid 5 digit numeric zip code.");

} else if (isNaN(zip)) { // Checks if the text field "zip" for alphabetic characters. The "isNaN" function is to determine if a value does not convert to a number, if so it returns an error message.
alert("Please enter a numeric value.");
}
}
</script>

格式:
<div class="searchZip">
<p>Please enter your zip code for availability</p>
<form>
<input type="text" name="zip" id="zip" placeholder="Zip Code">
<button onclick="getZip(event);">Check</button>
</form>
<p id="display"></p>
</div>

最佳答案

正如其他人所说,您可以使用break退出循环。

我不会使用循环。相反,我将使用 .some 函数来确定是否存在匹配项:

function getZip(e) {
e.preventDefault();
var zip = document.getElementById('zip').value; // Gets the value from the text field "zip" on the form.
var result = ["48650", "48611", "48746", "48614", "48706"];

if (zip === "") { // Checks if the text field "zip" is empty, if so it returns an error message.
alert('Please enter a zip code!');

} else if (zip.length > 5) {
alert("Please enter a valid 5 digit numeric zip code.");

} else if (zip.length < 5) {
alert("Please enter a valid 5 digit numeric zip code.");

} else if (isNaN(zip)) { // Checks if the text field "zip" for alphabetic characters. The "isNaN" function is to determine if a value does not convert to a number, if so it returns an error message.
alert("Please enter a numeric value.");
} else {
var match = result.some(function(r) {
return zip === r;
});

if (match) {
alert('Match ' + match);
} else {
alert("No Match");
}
}
}
<div class="searchZip">
<p>Please enter your zip code for availability</p>
<form>
<input type="text" name="zip" id="zip" placeholder="Zip Code">
<button onclick="getZip(event);">Check</button>
</form>
<p id="display"></p>
</div>


或者,您可以将 indexOf 用于像您这样的简单匹配。

关于javascript - 一旦在数组中找到匹配项,如何停止JavaScript循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33874466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com