【问题标题】:Adding an if statement inside if statement在 if 语句中添加 if 语句
【发布时间】:2011-11-30 13:47:47
【问题描述】:

我有一个提交表单的按钮的功能。此函数检查是否选中了 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 else 块放在第一个 if 块中,而不是尝试放在 if 语句中?
  • 请重新标记 - 您的问题是理解基本的编程概念、值得注意的布尔值和运算符。 jQuery在这里是无辜的!
  • $("#ctl") 可以有除"0""1" 以外的值吗?如果是这样,应该怎么办?

标签: javascript jquery forms validation


【解决方案1】:

怎么样:

if (!($finishBoxes.length == $finishBoxes.filter(':checked').length && 
    $('#tcaccept').val() == '1' && 
    !($("#ctl").val() == "1" && $('#ctlapp').val() === "")))

我们在这里添加的是另一个条件,即“确保#ctl 不是 1,#ctlapp 不是空白”。

编辑:请参阅我上面的评论 - 您的问题与 jQuery、表单或验证无关。这几乎与JS有关。

【讨论】:

  • 如果$("#ctl").val() == "1000"
  • @zuloo 虽然我强烈怀疑在实践中,$("#ctl").val() 唯一可能的值将是“0”和“1”。
  • @MatthewBrennd 忽略我的恶作剧,我只是心情不好。但是如果你能理解为什么你的问题不是关于 jQuery 的,那么你就在启蒙的道路上迈出了重要的一步。
  • @N3dst4:这就像一个魅力。如果我不问问题,我永远不会去任何地方学习。感谢您的建议。
【解决方案2】:
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;
}

【讨论】:

    【解决方案3】:

    我会说稍微清理一下代码,事情会变得简单一些:

    $('#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";
    });
    

    我放弃了对 $('#ctl').val() == "0" 的测试,因为这听起来像是它可以拥有的唯一其他值,并且如果它是 "0",则不需要进行验证。

    【讨论】:

    • 感谢您的建议,我现在可以使用它,但是当我有时间时,我会按照建议进行清理。我喜欢单独的警报。
    【解决方案4】:

    使用逻辑或|| 捕获两个星座:

    $("#ctl").val() == "1" && $('#ctlapp') != ""

    $("#ctl").val() == "0"

    if (
      !(
        $finishBoxes.length == $finishBoxes.filter(':checked').length 
      && 
        $('#tcaccept').val() == '1' 
      && 
        ( 
           ($("#ctl").val() == "1" && $('#ctlapp') != "")
        ||
           $("#ctl").val() == "0"
        )
      )
    )
    

    【讨论】:

    • 哇,如果有错误,我不想成为调试此语句的人。
    • 一旦您与团队中的其他几位开发人员一起工作,您将学会关心。这不是可维护的代码,难以阅读,让我想哭。
    • 我有很多同事,他们喜欢我缩进复杂 if 语句的方式。确保您可以将其分解成碎片并使代码更复杂(例如定义一个验证函数,该函数最终与语句相距 100 多行...),但我看不出您的问题出在哪里,绝对清楚部分条件开始和结束。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 2022-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多