[英]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.