我有一个提交表单的按钮功能。此函数检查是否选中了 5 个复选框#co1、#co2、#co3、#div1、#cc1。
然后它还会检查 tcaccept select 是否设置为 1。 如果是这样,则提交表单,否则弹出警报。
这是我目前的代码:
$('#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";
});
一切都很好,但我想添加到这一行,因为我有另一个需要的选项。但这需要是一个 if 语句。
if (!($finishBoxes.length == $finishBoxes.filter(':checked').length && $('#tcaccept').val() == '1' && THE IF STATEMENT))
这就是我需要合并到上面的 if 语句中的内容。
if ($("#ctl").val() == "1") {
$('#ctlapp') must not be blank
}
else if ($("#ctl").val() == "0") {
$('#ctlapp') can be blank
}
如有任何帮助,我们将不胜感激。
最佳答案
怎么样:
if (!($finishBoxes.length == $finishBoxes.filter(':checked').length &&
$('#tcaccept').val() == '1' &&
!($("#ctl").val() == "1" && $('#ctlapp').val() === "")))
我们在这里添加的是另一个条件,“并确保 #ctl
不是 1,并且 #ctlapp
不是空白”。
编辑:请参阅我上面的评论 - 您的问题与 jQuery、表单或验证无关。它几乎与 JS 无关。
关于javascript - 在 if 语句中添加 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8326932/