嵌套if语句vs&&操作员javascript jquery

nested if statement vs && operator javascript jquery

本文关键字:amp 操作员 javascript jquery if vs 嵌套 语句      更新时间:2023-09-26

我一直在使用&运算符,其中一个嵌套语句以旧形式工作,但一旦转换就停止工作。

我做错了吗?

考虑:

;(function( $, window ){
    var jsonData = <venda_block></venda_block>,
    isActive = jsonData.isActive, // false = off, true = on
    spendThreshold = jsonData.spendThreshold,
    displayThreshold = jsonData.displayThreshold,
    orderTotal = <venda_total>,
    remainingSpend = spendThreshold - orderTotal;
    $('.deliveryType').text(jsonData.deliveryType);
    if (isActive) {
    if (orderTotal >= displayThreshold) {
    if (!(orderTotal >= spendThreshold)) {
        $('.freeDeliveryBlurb2').show();
        $('.remainingSpend').html(remainingSpend);
    }}}
    }( jQuery, window ));

&amp;

if (isActive) {
    if (orderTotal >= displayThreshold && !(orderTotal >= spendThreshold)) {
        $('.freeDeliveryBlurb2').show();
        $('.remainingSpend').html(remainingSpend);
    }}
    }( jQuery, window ));

如果您想用&amp;:

if (isActive) {
if (orderTotal >= displayThreshold) {
if (!(orderTotal >= spendThreshold)) {
    $('.freeDeliveryBlurb2').show();
    $('.remainingSpend').html(remainingSpend);
}}}

结果可能是这样的:

if (isActive && orderTotal >= displayThreshold && !(orderTotal >= spendThreshold)) {
    $('.freeDeliveryBlurb2').show();
    $('.remainingSpend').html(remainingSpend);
}

或者,作为!(orderTotal>=spentThreshold)相当于(orderTotal<spentThrecture),如下所示:

if (isActive && orderTotal >= displayThreshold && orderTotal < spendThreshold) {
    $('.freeDeliveryBlurb2').show();
    $('.remainingSpend').html(remainingSpend);
}

请注意,如果语句写错了,您的原始代码中可能存在如下错误:

if (orderTotal >= displayThreshold {

而不是

if (orderTotal >= displayThreshold) {