Example:
例:
In this example, we are reading salary of an employee and finding the discount and net pay based on given salary and discount rate.
在此示例中,我们正在读取员工的薪水,并根据给定的薪水和折扣率找到折扣和净工资。
Code (JS & HTML):
代码(JS和HTML):
<!DOCTYPE html>
<HTML>
<HEAD>
<SCRIPT>
function Calculate(){
var sa=parseInt(document.getElementById("txta").value);
var disc=0.0;
var np=0.0;
if(sa>0){
if(sa>5000){
if(sa>15000){
if(sa>30000){
disc=sa*0.4;
}
else{
disc=sa*0.25;
}
}
else{
disc=sa*0.15;
}
}
else{
disc=sa*0.05;
}
document.getElementById("txtb").value=""+disc;
var np=sa-disc;
document.getElementById("txtc").value=""+np;
}
else{
alert('Invalid Sale Amount')
}
}
</SCRIPT>
</HEAD>
<BODY>
<h2>Nested If : True ladder</h2>
<hr />
<table>
<tr>
<td>
<label>Enter Sale Amount:</label>
</td>
<td>
<input type="text" name="txta" id="txta" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="button" value="Calculate Discount" onclick="Calculate()" />
</td>
</tr>
<tr>
<td>
<label>Discount</label>
</td>
<td>
<input type="text" name="txtb" id="txtb" readonly />
</td>
</tr>
<tr>
<td>
<label>Net Pay</label>
</td>
<td>
<input type="text" name="txtc" id="txtc" readonly />
</td>
</tr>
</table>
</BODY>
</HTML>
Output
输出量
翻译自: https://www.includehelp.com/code-snippets/nested-if-example-in-javascript.aspx