首页 > 解决方案 > 刀片模板 Laravel 中的不等式值错误

问题描述

我目前在我的 Blade.php (Laravel) 页面上使用 javascript 代码,我有一个奇怪的错误:我的 != 不被接受:

if({{ $CurrentProduct->disposable_quantity }} != null) {
    if (newQty > {{ $CurrentProduct->disposable_quantity }}) {
        $('input[name="quantity"]').val({{ $CurrentProduct -> disposable_quantity }});
    }
}

如果我放一个 == 我的代码不再包含错误。我不明白

标签: javascriptlaravel

解决方案


如果你真的想确认一个变量不是null而且不是empty string特别的,你会写:

if({{ $CurrentProduct->disposable_quantity }} !== null) {
   // do something
}

我更改了您的代码以检查类型是否相等(!==| ===)。
您还可以使用以下简单代码:

if(Boolean({{ $CurrentProduct->disposable_quantity }} )){ 
  // do something 
}

注意:直观上为empty, 0an empty string, null, undefined, andNaN的值变为
其他值变为


推荐阅读