复合IF语句为Javascript,函数不返回值
复合IF语句为Javascript,函数不返回值。我找不到语法错误,但我很确定这是一个简单的错误。这是课堂上使用的一个例子,它在那里运行,我的任务是做一些修改,现在它无法正确运行复合IF语句为Javascript,函数不返回值,javascript,Javascript,复合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
<?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>
信用卡支付
功能calcPay(账单)
{
你的工资=0;
if(paybill.paytype[0]。选中==true)
{
yourPay=(yourPay+50);
}
其他的
{
yourPay=(yourPay+0);
}
}
if(paybill.medtype[0]。选中==true)
{
yourPay=(yourPay+(余额*.10));
}
其他的
{
if(paybill.medtype[1]。选中==true)
{
yourPay=yourPay+bal.value;
}
其他的
{
yourPay=yourPay+其他P.value;
}
}
yourPay=Math.round(yourPay*100)/100;
退还你的工资;
}
计算工资
输入名称:
信用卡号码:
账户余额:
输入类型:
逾期付款
到期日前
输入所需的付款类型:
最低付款额
工资余额
其他金额:
您的付款方式为:
如果正确对齐选项卡,很容易看到错误所在
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;
}
功能计算(账单)
{
你的工资=0;
if(paybill.paytype[0]。选中==true)
{
yourPay=(yourPay+50);
}
其他的
{
yourPay=(yourPay+0);
}
}这不应该在这里!
if(paybill.medtype[0]。选中==true)
{
yourPay=(yourPay+(余额*.10));
}
其他的
{
if(paybill.medtype[1]。选中==true)
{
yourPay=yourPay+bal.value;
}
其他的
{
yourPay=yourPay+其他P.value;
}
}
yourPay=Math.round(yourPay*100)/100;
退还你的工资;
}
功能calcPay(账单)
{
var yourPay=0;//首先声明yourPay。
如果(!!paybill.paytype[0]。已选中)
{
yourPay=(yourPay+50);
}
如果(!!paybill.medtype[0]。已选中)
{
yourPay=(yourPay+(paybill.bal.value*.10));//账单是账单的一部分
}
其他的
{
如果(!!paybill.medtype[1]。选中)
{
yourPay=yourPay+paybill.bal.value;//账单是账单的一部分
}
其他的
{
yourPay=yourPay+paybill.otherP.value;//otherP是paybill的一部分
}
}
yourPay=Math.round(yourPay*100)/100;
退还你的工资;
}
如果没有对所有if/else块进行适当的缩进,则此代码完全不可读。事实上,如果进行适当的缩进,则很容易看到错误所在。您有两个额外的
}
,一个在每个else{}
块的末尾。编辑,对不起,就一个。第一个。仔细缩进代码!修正缩进后,问题应该很明显。如果选中的为真或假,则不需要使用代码>以转换为布尔值。已经是了。谢谢你,我得到了它的功能。
<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>
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;
}