我試圖在下面的表單中使用if語句來評估結果。當第二個結果除以第一個結果時,平均百分比就產生了。如果該百分比大於或等於55,結果應該返回「通過」,否則應該是「失敗」。不知道錯誤在哪裏與我的IF語句。Javascript - 用if語句形式評估結果
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script>
<script>
$(document).ready(function() {
$(".input").keyup(function() {
var val1 = +$(".value1").val();
var val2 = +$(".value2").val();
$("#result").val("$"+(val1/val2).toFixed(2));
var val3 = +$(".value3").val();
var val4 = +$(".value4").val();
$("#result2").val("$"+(val3/val4).toFixed(2));
$("#result3").val(((val3/val4)/(val1/val2)*100).toFixed(2) + "%");
var result = $("#result2").val()/$("#result").val();
if(result >= 55){
$("#TestResult").val((result * 100).toFixed(2) + "%" + "Pass");
} else {
$("#TestResult").val("FAIL");
}
});
});
</script>
<table>
<tr>
<td>Non-taxable benefits paid to Highly Compensated EE's:</td>
<td>
<input type="text" class="input value1">
</td>
</tr>
<tr>
<td>Number of Highly Compensated Employees:</td>
<td>
<input type="text" class="input value2">
</td>
</tr>
<tr>
<td>Average Benefits paid to Highly Compensated EE's:</td>
<td>
<input type="text" disabled="disabled" id="result">
</td>
</tr>
<tr>
<td>Non-taxable benefits paid to Non-Highly Compensated EE's:</td>
<td>
<input type="text" class="input value3">
</td>
</tr>
<tr>
<td>Number of Non-Highly Compensated Employees included*:</td>
<td>
<input type="text" class="input value4">
</td>
</tr>
<tr>
<td>Average Benefits paid to Non-Highly Compensated EE's:</td>
<td>
<input type="text" disabled="disabled" id="result2">
</td>
</tr>
<tr>
<td>Average Benefit Percentage Ratio:</td>
<td>
<input type="text" disabled="disabled" id="result3">
</td>
</tr>
<tr>
<td>Result:</td>
<td>
<input type="text" id="TestResult" style='overflow:hidden; border:0px solid #000000; width:80%'>
</td>
</tr>
前面有一個$符號,由前面的代碼加上 – Joe 2014-09-23 13:26:11