[英]How do you make a javascript “if” statement with both “and” and “or”?
I'm trying to make a page where you fill in some input boxes and check a radio button, and if you complete all of it, you can click a div, and animations happen. 我正在尝试制作一个页面,在其中填充一些输入框并选中一个单选按钮,如果全部完成,则可以单击一个div,然后发生动画。 The specific input boxes are not the only ones on the page.
特定的输入框不是页面上唯一的输入框。 I'm trying to use a javascript "if" statement that has a bunch of "and"'s and an "or" in parentheses, but when I open the page, the code doesn't run.
我正在尝试使用JavaScript“ if”语句,该语句在括号中有一堆“ and”和“ or”,但是当我打开页面时,代码无法运行。 This isn't all my code, and I know the javascript and it's libraries are linked because I've been coding this site for a while, and everything has worked up until now.
这不是我的全部代码,而且我知道javascript及其库已链接在一起,因为我已经对该站点进行了一段时间编码,并且到目前为止一切都已完成。 I checked the code in a javascript validator and it seemed fine.
我在javascript验证程序中检查了代码,看起来还不错。 I'm not sure what I'm doing wrong.
我不确定自己在做什么错。
$(document).ready(function(){
if ( $(".managementCompanyName").val() !== '' &&
$(".approvedBy").val() !== '' &&
$(".contractStartDate").val() !== '' &&
$(".proposalNumber").val() !== '' &&
$(!$("input[name='proposalReviewedForInvoice']:checked").val() || !$("input[id='proposalNotReviewedForInvoice']:checked").val()) ) {
//do stuff
}
});
Alternatively I have 或者我有
$(document).ready(function(){
if ( $(".managementCompanyName").val() !== "" &&
$(".approvedBy").val() !== "" &&
$(".contractStartDate").val() !== "" &&
$(".proposalNumber").val() !== "" &&
$("input[name='proposalReviewedForInvoice']:checked").val() !== "" ) {
//do stuff
}
});
This code seems to work on another part of the site where there's only one input as a requirement. 该代码似乎可以在网站的另一部分工作,因为只有一部分输入是必需的。 Thank you if you can spot my error.
谢谢,如果您能发现我的错误。
Wrap the ||
包装
||
part in parentheses, otherwise the first operand to ||
放在括号中,否则第一个操作数
||
is actually the last result from the last &&
. 实际上是最后一个
&&
的最后一个结果。
/*$*/(!$("input[name='proposalReviewedForInvoice']:checked").val() ||
!$("input[id='proposalNotReviewedForInvoice']:checked").val()) ) {
And actually it seems that you rather had them wrapped in a $()
, which will always return a jQuery object, which will always be "truthy" in the condition. 实际上,您似乎更希望将它们包装在
$()
,该对象将始终返回jQuery对象,该对象在条件中始终“真实”。
for handling errors much better if you only used the "OR (||) " condition. 如果仅使用“ OR(||)”条件,则可以更好地处理错误。
$(document).ready(function(){
var management = $(".managementCompanyName").val();
var approved = $(".approvedBy").val();
var contract = $(".contractStartDate").val();
var proposed_num = $(".proposalNumber").val();
var proposed_rev = $("input[name='proposalReviewedForInvoice']:checked").val();
if ( management == '' || approved == '' || contract == '' || proposed_num == ''
|| proposed_rev == '' ) {
// error message
} else {
// do stuff
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.