由 OR 运算符分隔的语句的计算顺序
Evaluation order of statements separated by an OR operator
这是我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
上取下或将文本字段留空,它可以正常工作。
您在检查它之前正在记录它。改变
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 将包含空白(只要一个为空白而另一个不是)。
只要至少true
一个条件,逻辑 OR 运算符就返回 true。这意味着,在您的情况下,如果其中一个值不为空,则始终执行 if
语句。这意味着即使id
为空,如果amenity
不为空,语句也会通过 - 这就是 OR 完成的。
事实上,由于逻辑 OR 运算符短路,它甚至不检查 id
的值:在表达式(amenity != '' || id != '')
中,左侧被计算。如果它的计算结果为 true
,则整个语句变为true
,并且if
块被计算;右侧将被忽略。
如果 amenity != ''
的计算结果为 false,则 OR 运算符仅返回 id != ''
。由于左侧为 false,因此仅右侧将确定整个表达式的值,因此这就是返回的内容。如果它是true
,则至少true
一个条件,并评估if
块。如果是 false
,它们都是,并且跳过if
块。
如果您真的希望仅针对非空 ID 评估代码,解决方案是仅检查 id:if (id != '')
。事实上,在 JavaScript 的真实性世界中,你可以简单地说if (id)
,这就是你所需要的。
if (id) {
// do ajax post
}
else {
// log errors
}
从你的问题中,我真的不知道你在找什么。听起来你对条件测试的评估感到沮丧。逻辑"or"运算符的意思是"如果此语句的任何项为真,则此语句为真"。因此,您正在检查"便利性","id"或两者是否"松散地等于"空字符串。这意味着如果分配了便利性、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');
修复了它。
不知道为什么每个人都建议AND
当我想要OR
.如果我只希望它在两个都为空白时失败,我会使用 &&
.我知道其中的区别...?
我希望无论#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.'); }
- 可以't让我的if语句处理js中的html表单输入
- 如何在ReactJS JSX中执行嵌套的if-else语句
- ajax请求的顺序总是不同的
- 按照选项卡索引的顺序循环一个jQuery选择
- 匹配一个单词,其中候选人可以跨越顺序组(跨度)
- 按顺序添加和删除类
- 自动修复”;语句前缺少分号“;
- 为什么 JQuery 附加颠倒了我的语句顺序
- SQL语句是通过PhoneGap'按顺序执行的吗;的Web SQL数据库
- 由 OR 运算符分隔的语句的计算顺序
- if 和 else 语句处理的顺序是什么
- 如何使用 javascript 按顺序运行数字语句
- NodeJS 顺序异步与条件语句
- Javascript 语句执行顺序
- 可以't获取if语句的正确顺序:将运行这两个语句
- 不同浏览器的控制台语句的执行顺序
- 在Javascript中有顺序执行语句的方法吗?
- Javascript没有按顺序执行if语句
- 在JavaScript语句中多次修改变量和函数参数的求值顺序
- if语句中表达式的顺序