比较Javascript中的2个值

时间:2015-08-03 07:30:33

标签: javascript

我在比较我的javascript中的值时遇到问题,这是代码:

     if(removeComma(f.txt_minTransFee.value) >= removeComma(f.txt_maxTransFee.value)){
         alert("MINIMUM TRANSFER FEE MUST BE LESS THAN MAXIMUM TRANSFER FEE");
         return;
     }
     if(removeComma(f.txt_minExFee.value) >= removeComma(f.txt_maxExFee.value)){
         alert("MINIMUM EXPRESS FEE MUST BE LESS THAN MAXIMUM EXPRESS FEE");
         return;
     }
     if(removeComma(f.txt_minEarsFee.value) >= removeComma(f.txt_maxEarsFee.value)){
         alert("MINIMUM EARLY SETTLEMENT FEE MUST BE LESS THAN MAXIMUM EARLY SETTLEMENT FEE");
         return;
     }

我试过的值是:

txt_minTransFee = 1 and txt_maxTransFee = 22
txt_minExFee = 2 and txt_maxExFee = 22
txt_minEarsFee = 3 and txt_maxEarsFee = 22

但问题是无法从第3个功能传递,它始终保持警觉 “最低早期解决费用必须低于最早的结算费用”

1 个答案:

答案 0 :(得分:0)

首先,将字符串解析为数字,处理无效输入的错误。然后,比较数字。

<?xml version="1.0" encoding="utf-8"?>
<set android:shareInterpolator="false"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromXScale="0.0"
        android:toXScale="1.0"
        android:fromYScale="0.0"
        android:toYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="false"
        android:duration="200" />

    <rotate
        android:duration="200"
        android:interpolator="@android:anim/linear_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fromDegrees="270"
        android:toDegrees="360" />
</set>

注意:以上内容会将 var minTransFee = +removeComma(f.txt_minTransFee.value); var maxTransFee = +removeComma(f.txt_maxTransFee.value); var minExFee = +removeComma(f.txt_minExFee.value); var maxExFee = +removeComma(f.txt_maxExFee.value); var minEarsFee = +removeComma(f.txt_minEarsFee.value); var maxEarsFee = +removeComma(f.txt_maxEarsFee.value); if (isNaN(minTransFee) || isNaN(maxTransFee) || isNaN(minExFee) || isNaN(maxExFee) || isNaN(minEarsFee) || isNaN(maxEarsFee)) { // Handle the error, an invalid number was input return; } if (minTransFee >= maxTransFee) { alert("MINIMUM TRANSFER FEE MUST BE LESS THAN MAXIMUM TRANSFER FEE"); return; } if (minExFee >= maxExFee) { alert("MINIMUM EXPRESS FEE MUST BE LESS THAN MAXIMUM EXPRESS FEE"); return; } if (minEarsFee >= maxEarsFee) { alert("MINIMUM EARLY SETTLEMENT FEE MUST BE LESS THAN MAXIMUM EARLY SETTLEMENT FEE"); return; } 视为""。如果您不想这样,则需要先检查0的字符串,然后再通过""转换为数字。

或者:每当你有这样的重复代码时,请考虑将公共部分分解为函数:

+

...我们可以这样使用:

function checkMinMax(f, type, msg) {
     var minFeeStr = removeComma(f["txt_min" + type + "Fee]".value.trim());
     var maxFeeStr = removeComma(f["txt_max" + type + "Fee]".value.trim());
     var minFee = +minFeeStr;
     var maxFee = +maxFeeStr;

     if (!minFeeStr || !maxFeeStr || isNaN(minFee) || isNaN(maxFee)) {
         // One of them was blank or invalid
         // Show message
         return false;
     }

     if (minFee >= maxFee) {
         alert(msg);
         return false;
     }

     return true;
}

请注意,该版本会检查 if (!checkMinMax(f, "Trans", "MINIMUM TRANSFER FEE MUST BE LESS THAN MAXIMUM TRANSFER FEE") || !checkMinMax(f, "Ex", "MINIMUM EXPRESS FEE MUST BE LESS THAN MAXIMUM EXPRESS FEE") || !checkMinMax(f, "Ears", "MINIMUM EARLY SETTLEMENT FEE MUST BE LESS THAN MAXIMUM EARLY SETTLEMENT FEE")) { return; } 并认为它们无效。