用单选按钮计算Javascript / HTML5的麻烦

时间:2014-03-06 00:23:34

标签: javascript html5 algorithm radio

正在为这个HTML5片段开发Javascript,但是我无法正确地获得美联储税或总收入。我认为这可能与我的单选按钮有关。任何帮助都会很精彩。我是新手,所以请尽量以外行的方式保留答案。

<!HTML>
<html>
<head>
<title> Payroll Calculator </title>
<script language="javascript">
  function Calc()
  {
    var hour, pay, status, kids, gross, tax, net;
    hour = parseFloat(document.payroll.Hours.value);
    pay = parseFloat(document.payroll.PayRate.value);
    gross = parseFloat (hour * pay);
    document.payroll.GrossPay.value = gross.toFixed(2);
    kids = parseFloat(document.payroll.Dependents.value);
    status = parseFloat(document.payroll.Marital.value);
    tax = parseFloat (gross * status) - kids;
    document.payroll.FedTaxes.value = tax.toFixed(2);
    net = parseFloat (gross - tax);
    document.payroll.NetPay.value = net.toFixed(2);
  }
</script>
</head>
<body>

<form name="payroll">
<table bgcolor="grey" cellpadding="0">
<tr><td colspan="2" align="center"><b>Payroll Calculator<b></td></tr>

<tr><td><label for="name"> Name: </label></td>
<td><input type="text" name="name"></td></tr>

<tr><td><label for="Hours"> Hours: </label></td>
<td><input type="text" name="Hours"> 99.9 </td></tr>

<tr><td><label for="PayRate"> Pay Rate: </label></td>
<td><input type="text" name="PayRate"> 99.99 </td></tr>

<tr><td colspan="2"> Marital Status: 
<input type="radio" value=".30" name="Marital" checked> Single
<input type="radio" value=".20" name="Marital"> Married </td></tr>

<tr><td> Dependents: </td><td>
<select name="Dependents">
    <option value="10"> 1 </option>
    <option value="20"> 2 </option>
    <option value="30"> 3 </option>
    <option value="40"> 4 </option>
    <option value="50"> 5 </option>
</select></td></tr>

<tr><td><label for="GrossPay"> Gross Pay: </label></td>
<td><input type="text" name="GrossPay" readonly></td></tr>

<tr><td><label for="FedTaxes"> Fed Taxes: </label></td>
<td><input type="text" name="FedTaxes" readonly></td></tr>

<tr><td><label for="NetPay"> Net Pay: </label></td>
<td><input type="text" name="NetPay" readonly></td></tr>

<tr><td colspan="2" align="center"><input type="button" value="Calculate" onclick="Calc()">
<input type="reset" value="Clear"></td></tr>
</form>

</table>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

要获取所选无线电的值,您需要检查哪一个被检查并获得它们的值。

所以你可以用以下方法计算状态变量:

status = parseFloat(document.payroll.Marital[0].checked ? document.payroll.Marital[0].value : document.payroll.Marital[1].value);

您可以在此处查看:http://jsbin.com/yekadohu/1/

问候!