简体   繁体   English

如果使用变量时IF不起作用

[英]IF does not work when using variables

The second if does not work, and the third one does not work if I put a variable instead of "2" I was trying to put if (cont==len) but does not work. 第二个if不起作用,而第三个if不起作用,如果我放置一个变量而不是我试图放入if(cont == len)但不起作用的“ 2”。 Which is the problem? 这是什么问题?

function alert(Vform){
var i=0;
var cont=0;
var len=Vform.length;
for (i=0;i<=len;i++){
    if (Vform.elements[i].checked!=true){
    cont=cont+1
    }
}
if (cont!=2){
    window.alert("Please select date and time");
}

} }

Try to do these changes: 尝试进行以下更改:

function alert(Vform)
{
var cont=0;
var len=Vform.length;
for (var i=0;i<=len;i++)
{
    if (Vform.elements[i].checked!=true)
    {
        cont++;
    }
}
if (cont != 2)
{
    alert("Please select date and time");
}
}

Your for loop should be like this. 您的for循环应如下所示。

for (i=0;i<len;i++){
   //code
}

It should check i<len and not i<=len because array element start with zero based index 它应该检查i<len而不是i<=len因为数组元素以从零开始的索引开头

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么此代码仅在变量位于 function 内时才有效? - Why does this code only work when the variables are inside the function? 使用when的简单joi模式不起作用 - Simple joi schema using when does not work AngularJS 如何知道变量何时发生变化? AngularJS 脏检查是如何工作的? - How does AngularJS know when variables change? How does AngularJS dirty checking work? Angular 使用定时器时看不到变量值 - Angular does not see variables values when using timer 将会话变量用于数据时,将预览打印为空白。 局部变量可以正常工作 - Print preview blank when using session variables for data. Local variables work fine 当只有一个参数但有两个变量时,闭包如何工作? - How does a closure work when there's only one parameter but two variables in it? 在另一个div上使用touch时,contenteditable不起作用 - contenteditable does not work when using touch on another div 使用jQuery addClass添加类时,脚本不起作用 - The script does not work when adding a class using jQuery addClass 为什么 Mongoose updateMany() 仅在使用 .then 时有效 - Why does Mongoose updateMany() only work when using .then 使用 Fetch API 时数据流如何工作? - How does data flow work when using the Fetch API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM