我试图在变量中添加条件,然后在 if() 条件中赋值,但它没有按预期工作。
尝试过的可能性:
1)
conditionCheck = (getMonth == undefined || getMonth == "" || getMonth == null
|| getDay == undefined || getDay == "" || getDay == null
|| getYear == undefined || getYear == "" || getYear == null )
2)
conditionCheck = getMonth == undefined || getMonth == "" || getMonth == null
|| getDay == undefined || getDay == "" || getDay == null
|| getYear == undefined || getYear == "" || getYear == null
3)
conditionCheck = "getMonth == undefined || getMonth == "" || getMonth == null
|| getDay == undefined || getDay == "" || getDay == null
|| getYear == undefined || getYear == "" || getYear == null"
但如果我按原样添加,则它可以正常工作。
像这样。 ->
if ( getMonth == undefined || getMonth == "" || getMonth == null
|| getDay == undefined || getDay == "" || getDay == null
|| getYear == undefined || getYear == "" || getYear == null ) {
} else {
ageCalculation();
}
任何想法/建议?
编辑:
function ageCalculation() {
getDate = getYear + "-" + getMonth + "-" + getDay;
dob = new Date(getDate);
today = new Date();
age = (today - dob) / (365.25 * 24 * 60 * 60 * 1000);
if (age < 3 || age == 3 || age > 3 && age < 3.00452422294471007) {
$('.greater-msg, .less-then-msg').remove();
$(contentParent).find('.fieldset-wrapper').after('<div class="less-then-msg">Disclaimer: In compliance with EO51, cannot directly engage with mothers with children aged 0 to 3 years old. All content that you will receive via email will only be regarding your pregnancy. </div>');
} else if (age > 3) {
$('.greater-msg, .less-then-msg').remove();
$(contentParent).find('.fieldset-wrapper').after('<div class="greater-msg">You can also visit to know how you can keep giving your child the 360 advantage.</div>');
}
if (age <= -1 || age <= -0 || age == 0 || age == -0) {
$('.greater-msg, .less-then-msg').remove();
}
}
谢谢!!
最佳答案
TL;DR; 在 JS 中,检查 variable 是否既不是 null、undefined 也不是 empty 只是由,
if (variable) {
// 'variable' is not null, undefined or ''.
}
您的第一个 和第二个 条件应该可以正常工作。
Be aware, Date.getMonth() returns 0-based index of the month. And if it just happens that month is
January, your condition will no longer work sincegetMonthis0and will turn yourconditionCheckto be alwaystrue.Because
0 == "".
至于您的第三个 条件,它是一个字符串,当包含在if 条件中时将始终返回true。
另一种方式,
因此,如果 getMonth、getDay 和 getYear 是 ,那么您要运行 ageCalculation 函数不是 undefined、null 或empty。
所以,而不是 (您当前的代码)
if (getMonth == undefined || getMonth == "" || getMonth == null || getDay == undefined || getDay == "" || getDay == null || getYear == undefined || getYear == "" || getYear == null) {
// ...
} else {
ageCalculation();
}
正如我上面所说,为了检查变量是 undefined 还是 null 甚至是 ""(empty),你可以修改你的代码,
if (getMonth && getDay && getYear) {
// getMonth, getDay, and getYear are not null, undefined or "".
ageCalculation();
}
else {
// Either getMonth, getDay and getYear have a value of null, undefined or "".
}
关于javascript - 一个变量中的 jQuery 多个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37518032/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击