【问题标题】:Evaluation order of statements separated by an OR operator由 OR 运算符分隔的语句的评估顺序
【发布时间】:2013-11-17 04:43:54
【问题描述】:

这是我的if 声明:

还有一个小提琴:http://jsfiddle.net/setMa/

$('#addamenity').click(function () {
    var id = $('.editblock').find('#id').val();
    var amenity = $("#amenity").val();

    var dataString = {
        id: id,
        amenity: amenity
    };
    console.log(dataString);
    if (amenity != '' || id != '') {
        $.ajax({
            type: "POST",
            url: "classes/add_amenities.php",
            data: dataString,
            cache: false,
            async: false,
            success: function (html) {
                $('.editunitamenities').load('classes/display_amenities.php?id=' + id);
            }
        });
    } else {
        alert('You must SAVE your changes to the new unit before adding amenities.');
    }
});

现在,我的控制台日志清楚地显示了 "" 的 id 是什么?如果我将 ||if 中删除,或者将文本字段留空,它就可以正常工作。

【问题讨论】:

  • 我不确定你在说什么。 $('.editblock').find('#id').val() 的值是空字符串吗?通常使用JSFiddle 之类的服务很有用,这样其他人也能看到您的问题。
  • @Qantas 94 Heavy 添加了小提琴。 @bfavaretto 不,因为#amenity 可能不是空白的。
  • 所以要输入 if 除非两个值都为空?
  • 我已修复,您可以阅读我的答案以更好地了解我想要什么
  • 是的,在 amenity != "" || foo 中,如果设置了 amenity,foo 的值将被完全忽略。

标签: javascript boolean-logic or-operator


【解决方案1】:

您在检查之前记录它。 改变

var dataString ={id:id,amenity:amenity};
console.log(dataString);    
if (amenity != '' || id != '')
{

if (amenity != '' && id != '')
{
    var dataString ={id:id,amenity:amenity};
    console.log(dataString);    

你想要 AND(不是 OR),否则你的 dataString 将包含空白(只要一个是空白而另一个不是)。

【讨论】:

    【解决方案2】:

    只要至少一个条件是true,逻辑或运算符就会返回真。这意味着,在您的情况下,如果 其中一个 值不为空,则将始终执行 if 语句。这意味着即使id 为空,如果amenity 不为空,语句也会通过——这就是OR 完成的。

    事实上,因为逻辑或运算符是短路的,它甚至不检查id 的值:在表达式(amenity != '' || id != '') 中,左侧被求值。如果计算结果为true,则整个语句变为true,并计算if 块;右侧被忽略。

    如果amenity != '' 的计算结果为假,OR 运算符只返回id != ''。由于左边是假的,所以只有右边会决定整个表达式的值,所以这就是返回的值。如果是true,则至少一个条件是true,并评估if 块。如果是false,两者都是,跳过if块。

    如果您真的希望仅针对非空白 ID 评估您的代码,解决方案是仅检查 id:if (id != '')。事实上,在 JavaScript 的真实世界中,你可以简单地说 if (id),这就是你所需要的。

    if (id) {
        // do ajax post
    }
    else {
        // log errors
    }
    

    【讨论】:

      【解决方案3】:

      根据您的问题,我真的无法判断您在寻找什么。听起来您对条件测试的评估感到沮丧。逻辑“或”运算符的意思是“如果其中任何一项为真,则该陈述为真”。因此,您正在检查“amenity”、“id”或两者是否“松散地等于”空字符串。这意味着如果 amenity、id 或两者都被分配,那么“if”语句块中的任何内容都将被执行。下面有一个小示例代码可以向您展示,但同样,很难说出您所追求的是什么。

      function aTest (amenity, id) {
          aTest.count += 1;
          console.log('### Test # ', aTest.count);
          console.log('amenity = ', amenity);
          console.log('id = ', id);
          if (amenity != '' || id != '') {
              console.log('amenity, id, or both: ' +
                          'is not kinda equal to an empty string');
          }
      }
      aTest.count = 0;
      
      aTest('', '');
      aTest('', 'x');
      aTest('x', '');
      aTest('x', 'x');
      

      【讨论】:

        【解决方案4】:

        修复它。

        不知道为什么当我想要OR 时每个人都建议AND。如果我只想在两者都为空白时失败,我会使用&&。我知道区别...?

        无论#amenity 的值如何,我都希望.ajax() 不运行。我不知道为什么它不起作用。相反,由于我不在乎 #amenity 中的内容,我决定为什么要检查它?我检查了一下,一切正常。

        if (id != '')
            {
                var dataString ={id:id,amenity:amenity};
                console.log(dataString);    
                $.ajax({        
                    type: "POST",
                    url: "classes/add_amenities.php",
                    data: dataString,
                    cache: false,
                    async:false,
                    success: function(html)
                    {
                        //$('.editunitamenities').html(html);
                        $('.editunitamenities').load('classes/display_amenities.php?id='+id);
                    }
                });
                //$('.editunitamenities').load('.editunitamenities');
            }else { alert('You must SAVE your changes to the new unit before adding amenities.'); }
        

        【讨论】:

        • “无论#amenity 的值如何,我都希望 .ajax() 不运行” 在您的 Fiddle 中,“Id”字段为空白,无法设置。所以....
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-30
        • 1970-01-01
        • 2014-01-17
        • 2021-03-05
        相关资源
        最近更新 更多