Compound IF statement it Javascript, Function not returning the value. I cant find the syntax error, but I am pretty sure it is a simple one. This was used as an example in class and it ran there, I was tasked to make some modifications and now it wont run correctly.
复合IF语句它Javascript,函数没有返回值。我找不到语法错误,但我很确定这是一个简单的错误。这在课堂上被用作一个例子,它在那里运行,我的任务是进行一些修改,现在它不能正常运行。
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Credit Card Payment</title>
<script type="text/javascript">
function calcPay(paybill)
{
yourPay = 0;
if (paybill.paytype[0].checked == true)
{
yourPay = (yourPay + 50);
}
else
{
yourPay = (yourPay + 0);
}
}
if (paybill.medtype[0].checked == true)
{
yourPay = (yourPay + (bal.value * .10));
}
else
{
if (paybill.medtype[1].checked == true)
{
yourPay = yourPay + bal.value;
}
else
{
yourPay = yourPay + otherP.value;
}
}
yourPay = Math.round(yourPay*100)/100;
return yourPay;
}
</script>
</head>
<body>
<h1>Calc Pay</h1>
<form name="paybill">
<p>Enter name:
<input type = "text" name="payname"></p>
Credit Card Number:
<input type = "text" name="cardNo"></p>
<p>Account Balance: <input type = "text" name="bal">
<p>Enter type:<br />
<input type = "radio" name="paytype" value="salary">Late Payment<br />
<input type = "radio" name="paytype" value="hourly">Prior to Due Date</p>
</p>
<p>Enter Desired Payment Type:<br />
<input type = "radio" name="medtype">Minimum Payment<br />
<input type = "radio" name="medtype">Pay Balance<br />
<input type = "radio" name="medtype">Other Amount: <input type = "text"
name="otherP"></p>
<input type="button" value="Calculate your payment" onclick="pay.value=calcPay
(paybill)" />
<b><i>Your payment is:</i></b> <input type="text" name="pay" size="10" /><be />
</form>
</body>
</html>
3 个解决方案
#1
<script type="text/javascript">
function calcPay(paybill)
{
var yourPay = 0; // first declare yourPay.
if (!!paybill.paytype[0].checked)
{
yourPay = (yourPay + 50);
}
if (!!paybill.medtype[0].checked)
{
yourPay = (yourPay + (paybill.bal.value * .10)); //bill is part of paybill
}
else
{
if (!!paybill.medtype[1].checked)
{
yourPay = yourPay + paybill.bal.value; //bill is part of paybill
}
else
{
yourPay = yourPay + paybill.otherP.value;//otherP is part of paybill
}
}
yourPay = Math.round(yourPay*100)/100;
return yourPay;
}
</script>
#2
If you align your tabs properly, it's easy to see where the error is.
如果正确对齐标签,则很容易看出错误的位置。
function calcPay(paybill)
{
yourPay = 0;
if (paybill.paytype[0].checked == true)
{
yourPay = (yourPay + 50);
}
else
{
yourPay = (yourPay + 0);
}
} // < This should not be here!
if (paybill.medtype[0].checked == true)
{
yourPay = (yourPay + (bal.value * .10));
}
else
{
if (paybill.medtype[1].checked == true)
{
yourPay = yourPay + bal.value;
}
else
{
yourPay = yourPay + otherP.value;
}
}
yourPay = Math.round(yourPay*100)/100;
return yourPay;
}
#3
http://jsfiddle.net/5oh6533L/1/
function calcPay (paybill) {
var yourPay = 0;
if (paybill.paytype[0].checked) {
yourPay += 50);
}
// this does nothing
// else yourPay = (yourPay + 0);
if (paybill.medtype[0].checked) {
yourPay += (bal.value * .10);
}
else if (paybill.medtype[1].checked) {
yourPay += bal.value;
}
else yourPay += otherP.value;
return Math.round(yourPay * 100) / 100;
}
#1
<script type="text/javascript">
function calcPay(paybill)
{
var yourPay = 0; // first declare yourPay.
if (!!paybill.paytype[0].checked)
{
yourPay = (yourPay + 50);
}
if (!!paybill.medtype[0].checked)
{
yourPay = (yourPay + (paybill.bal.value * .10)); //bill is part of paybill
}
else
{
if (!!paybill.medtype[1].checked)
{
yourPay = yourPay + paybill.bal.value; //bill is part of paybill
}
else
{
yourPay = yourPay + paybill.otherP.value;//otherP is part of paybill
}
}
yourPay = Math.round(yourPay*100)/100;
return yourPay;
}
</script>
#2
If you align your tabs properly, it's easy to see where the error is.
如果正确对齐标签,则很容易看出错误的位置。
function calcPay(paybill)
{
yourPay = 0;
if (paybill.paytype[0].checked == true)
{
yourPay = (yourPay + 50);
}
else
{
yourPay = (yourPay + 0);
}
} // < This should not be here!
if (paybill.medtype[0].checked == true)
{
yourPay = (yourPay + (bal.value * .10));
}
else
{
if (paybill.medtype[1].checked == true)
{
yourPay = yourPay + bal.value;
}
else
{
yourPay = yourPay + otherP.value;
}
}
yourPay = Math.round(yourPay*100)/100;
return yourPay;
}
#3
http://jsfiddle.net/5oh6533L/1/
function calcPay (paybill) {
var yourPay = 0;
if (paybill.paytype[0].checked) {
yourPay += 50);
}
// this does nothing
// else yourPay = (yourPay + 0);
if (paybill.medtype[0].checked) {
yourPay += (bal.value * .10);
}
else if (paybill.medtype[1].checked) {
yourPay += bal.value;
}
else yourPay += otherP.value;
return Math.round(yourPay * 100) / 100;
}