javascript 在 if 语句中添加 if 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8326932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding an if statement inside if statement
提问by Codded
I have a function for a button which submits a form. This function checks to see if the 5 checkboxes are selected #co1,#co2,#co3,#div1,#cc1.
我有一个提交表单的按钮的功能。此函数检查是否选中了 5 个复选框 #co1、#co2、#co3、#div1、#cc1。
It then also checks to see if tcaccept select is set to 1. If so the form submits, else the alert pops up.
然后它还会检查 tcaccept select 是否设置为 1。如果是,则表单提交,否则弹出警报。
This is the code i have at the moment:
这是我目前的代码:
$('#submit2pd').click(function(event) {
var $finishBoxes = $('#co1,#co2,#co3,#div1,#cc1');
if (!($finishBoxes.length == $finishBoxes.filter(':checked').length && $('#tcaccept').val() == '1' )) {
alert('Please complete the Induction Checklist confirming that you have read and understood the Colleges main policies and procedures, agreeing to comply with these in line with the terms of your contract with the College');
return false;
}
// otherwise the form is submitted
window.location.href = "submit2pd.php";
});
All works brilliantly, but i want to add to this line as i have another option that is required. But this needs to be an if statement.
一切都很好,但我想添加到这一行,因为我有另一个需要的选项。但这需要是一个 if 语句。
if (!($finishBoxes.length == $finishBoxes.filter(':checked').length && $('#tcaccept').val() == '1' && THE IF STATEMENT))
this is what i need to incorporate into the if statement above.
这是我需要合并到上面的 if 语句中的内容。
if ($("#ctl").val() == "1") {
$('#ctlapp') must not be blank
}
else if ($("#ctl").val() == "0") {
$('#ctlapp') can be blank
}
Any help would be greatly appreciated.
任何帮助将不胜感激。
采纳答案by N3dst4
How about:
怎么样:
if (!($finishBoxes.length == $finishBoxes.filter(':checked').length &&
$('#tcaccept').val() == '1' &&
!($("#ctl").val() == "1" && $('#ctlapp').val() === "")))
What we're adding here is another condition which says, "And make sure it's not the case that #ctl
is 1, and #ctlapp
is blank".
我们在这里添加的是另一个条件,它说,“并确保不是#ctl
1,#ctlapp
而是空白”。
Edit: and please see my comment above - your question is not about jQuery, forms, or validation. It's barely about JS.
编辑:请参阅我上面的评论 - 您的问题与 jQuery、表单或验证无关。这几乎是关于 JS 的。
回答by hungryMind
if($('#tcaccept').val() == '1' && validate()) {
// do something
}
var validate = function(){
if ($("#ctl").val() == "1") {
$('#ctlapp') must not be blank
return false;
}
else if ($("#ctl").val() == "0") {
$('#ctlapp') can be blank;
return true;
}
return true;
}
回答by RightSaidFred
I'd say clean up the code a little, and things will get a bit simpler:
我会说稍微清理一下代码,事情就会变得简单一点:
$('#submit2pd').click(function(event) {
var fail_message = 'Please complete the Induction Checklist confirming that you have read and understood the Colleges main policies and procedures, agreeing to comply with these in line with the terms of your contract with the College',
$finishBoxes = $('#co1,#co2,#co3,#div1,#cc1'),
$checkedBoxes = $finishBoxes.filter(':checked'),
tcaccept = $('#tcaccept').val(),
ctl = $("#ctl").val(),
ctlapp = $('#ctlapp').val();
if (tcaccept == '1' && $finishBoxes.length != $checkedBoxes.length ) {
alert( fail_message );
return false;
} else if( ctl == "1" && ctlapp == "" ) {
alert( "some other message" );
return false;
}
// otherwise the form is submitted
window.location.href = "submit2pd.php";
});
I left the test for $('#ctl').val() == "0"
out because it sounds like that's the only other value it can have, and there's no validation that needs to take place if it is "0"
.
我放弃了测试,$('#ctl').val() == "0"
因为听起来这是它可以拥有的唯一其他值,如果它是"0"
.
回答by zuloo
Use a logic or ||
to capture both constalations:
使用逻辑 or||
来捕获两个星座:
$("#ctl").val() == "1" && $('#ctlapp') != ""
$("#ctl").val() == "1" && $('#ctlapp') != ""
OR
或者
$("#ctl").val() == "0"
$("#ctl").val() == "0"
if (
!(
$finishBoxes.length == $finishBoxes.filter(':checked').length
&&
$('#tcaccept').val() == '1'
&&
(
($("#ctl").val() == "1" && $('#ctlapp') != "")
||
$("#ctl").val() == "0"
)
)
)