0

这是我的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或将文本字段留空,它就可以正常工作。

4

4 回答 4

1

From your question I really can't tell what you're looking for. It sounds like you're frustrated with the evaluation of your conditional test. The logical "or" operator means "this statement is true if any of its terms are true". So you're checking if "amenity", "id", or both of them are not "loosely equal to" the empty string. That means if amenity, id, or both of them are assigned then whatever is in your "if" statement block will be executed. There's a little example code below to show you but, again, it's difficult to tell what it is you're after.

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');
于 2013-11-18T00:47:42.057 回答
1

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

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

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

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

if (id) {
    // do ajax post
}
else {
    // log errors
}
于 2013-11-17T05:46:28.930 回答
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 将包含空白(只要一个为空白而另一个不是)。

于 2013-11-17T04:49:03.160 回答
-1

解决它。

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

.ajax()无论#amenity. _ 我不知道为什么它不起作用。相反,既然我不在乎里面有什么,#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.'); }
于 2013-11-17T04:55:09.347 回答