gpt4 book ai didi

javascript - 循环遍历 javascript 函数

转载 作者:行者123 更新时间:2023-11-28 20:58:54 24 4
gpt4 key购买 nike

我正在尝试在函数的 while 循环内循环 if 语句。但它只会命中第一个 if 语句并停止循环。

示例:

while(No.length == 0 || Name.length == 0 || Tel.length == 0 
|| Date.length == 0 || Email.length == 0) {

alert("Don't leave blank!");

if (No.length == 0) {
document.getElementById('Nos').style.visibility = 'visible';
return false;
}

if(Name.length == 0) {
document.getElementById('Name').style.visibility = 'visible';
return false;
}
//continues same if statement for rest of the elements variables.
}

它只会转到第一个 if 语句,不会循环遍历它。

最佳答案

您正在从循环内部返回;这打破了循环。如果您想继续进行下一轮循环,请改用 continue。如果您想跳出循环,但不从整个函数返回,请使用 break

现在,如果您使用 jQuery 循环,因为它实际上只是一个函数,所以您可以使用 return:

$.each([1,2,3,4], function(index, x) {
if (x < 4) return true; // equivalent to continue
if (x == 4) return false; // equivalent to break
});

但这仅适用于 jQuery 循环,不适用于 Javascript 标准循环。

关于javascript - 循环遍历 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11546128/

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