Javascript中不进入if条件的程序流
Flow of a program not going inside if condition in Javascript
下面是我的代码:
<script>
function monthassign()
{
document.getElementById("month").selectedIndex=0;
}
function isleap()
{
var yr=document.getElementById("year").value;
if ((parseInt(yr)%4) == 0)
{
if (parseInt(yr)%100 == 0)
{
if (parseInt(yr)%400 != 0)
{
//alert("Not Leap");
return "false";
}
if (parseInt(yr)%400 == 0)
{
//alert("Leap");
return "true";
}
}
if (parseInt(yr)%100 != 0)
{
//alert("Leap");
return "true";
}
}
if ((parseInt(yr)%4) != 0)
{
//alert("Not Leap");
return "false";
}
}
function dateassign()
{
var yr=isleap();
var mth=parseInt(document.getElementById("month").selectedIndex);
var dt=document.getElementById("date")
if(yr)
{
if(mth==2)
{
//alert(yr);
dt.options.length = 0;
for(i=1; i<30; i++)
{
dt.add(new Option(i,i), null) //add new option to end of "date"
}
return;
}
}
if(yr==false && mth==2)
{
//alert("Second fun");
dt.options.length = 0;
for(i=1; i<29; i++)
{
dt.add(new Option(i,i), null) //add new option to end of "date"
}
return;
}
if(mth==4 || mth==6 || mth==9 || mth==11)
{
dt.options.length = 0;
for(i=1; i<31; i++)
{
dt.add(new Option(i,i), null) //add new option to end of "date"
}
return;
}
else
{
dt.options.length = 0;
for(i=1; i<32; i++)
{
dt.add(new Option(i,i), null) //add new option to end of "date"
}
return;
}
}
</script>
我的问题是当变量yr包含假值时,第一个if条件在function dateassign()
中执行。当yr包含假值时,期望将程序控制转移到代码块if(yr==false && mth==2)
,但它没有发生。我受够了执行特定条件的问题,即使条件是假的,为什么控制不进入特定的if条件。请帮我解决这个问题。
true
和false
与"true"
和"false"
不是一回事。第一个是布尔值,第二个是字符串。您应该返回布尔值,因此您需要将"true"
的每个实例替换为true
,将"false"
的每个实例替换为false
。
相关文章:
- 如何在条件成立后停止程序执行
- 在谷歌应用程序脚本中,如何在满足条件的情况下只发送一次电子邮件
- 根据角度 js 应用程序中的条件动态添加类
- 基于淘汰中的条件的动态事件处理程序
- 使用chrome扩展程序在后台自动进行条件浏览
- 在 AngularJS 应用程序中有条件地注入模块
- 车把,如何为帮助程序返回的值设置条件
- 具有多个“数据”事件处理程序和竞争条件的 NodeJS 流
- 使用帮助程序的流星条件 HTML
- 嵌入闪亮应用程序中的数据表的条件选择类型
- angularjs将加载应用程序延迟到指定条件
- AngularJS在应用程序页面的导航栏中有条件地显示登录/注销按钮
- Handlebars.js自定义条件帮助程序
- Meteor——将单击处理程序绑定到条件中的按钮
- 在Windows 8 Metro Javascript应用程序中构建特定的条件符号
- Selenium Web驱动程序的执行由于javascript等待条件而陷入停滞
- 谷歌应用程序脚本条件if语句
- If条件在我的路由提供程序中
- 在特定条件下停止单击事件处理程序
- 扩展jQuery点击处理程序(修补$.fn.click函数?)以检查条件