0

我很难在我为一个班级设计的看似简单的 HTML/Javascript 页面中找到错误。我花了几个小时查看此页面的代码(以及其他 3 个让我发疯的代码)。我想知道是否有一些语法被我漏掉了?在此页面中,我们将编写一个脚本来破译客户是否超过了他们的信用额度。在此页面和其他 3 个页面中,onclick 事件似乎不起作用。这是完整的代码:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title>Homework 3 Part 3</title>
<style type="text/css">
tr.visiting {background-color: #000000; }


h1.header {color: #333333;} 
.creditbox { 

    background-color: #ffcc66;
    width: 60%;
    border:solid 5px #666666;
    margin-left:auto;
margin-right:auto;

}

table {
    text-align: right;
}
td.visiting {background-color: #000000; }
input {text-align: right;}
</style>
<script type="text/javascript">
/* <![CDATA[ */

function creditCalc() 
{
    var account, credLim, begBalance, totalCharged, credit, balance, endBalance;

    account = parseInt(document.creditLimitForm.accountNum.value);
    credLim = parseInt(document.creditLimitForm.creditLimit.value);
    begBalance = parseInt(document.creditLimitForm.beginningBalance.value);
    totalCharged = parseInt(document.creditLimitForm.amtCharged.value);
    credit = parseInt(document.creditLimitForm.creditsApplied.value);   

    balance = begBalance + totalCharged;
    endBalance = balance - credit;

    documemt.creditLimitForm.creditDisplay.value("Account Number:  " + account + "\nCredit Limit:  " + credLim + "\nBeginning Balance: " + begBalance + "\nTotal Charged this month:  " + totalCharged + "\nTotal credits this month:  " + credit + "\nCredit Available:  " + endBalance);

document.getElementById("creditAvailable").innerHTML = endBalance;

}

/*]]>*/
</script>

</head>

<body bgcolor = "#ffffff" alink = "#FFFFCC" link = "#FFFFCC" vlink = "#FFFFCC">
<table id="border" width="70%" align ="center" cellpadding="0">
        <tr>
        <td align="center">
            <img src="hw3Banner.jpg" alt="Homework Banner" align="middle" />
        </td>
        </tr>
    </table>
    <br />
    <table border="1" width="500" align ="center" bgcolor="#666666" cellpadding="2.5">
     <tr><td align = "center"><font face ="baskerville" color="#FFFFCC" /><a href="homework3.html">HW 3 Main</a></td>
     <td align = "center"><font face ="baskerville" color="#FFFFCC" /><a href="homework3pt1.html">Part 1</a></td>
     <td align = "center"><font face ="baskerville" color="#FFFFCC" /><a href="homework3pt2.html">Part 2</td>
     <td class="visiting" align = "center"><font face ="baskerville" color="#FFFFCC" /><a href="homework3pt3.html">Part 3</a></td>
    <td align = "center"><font face ="baskerville" color="#FFFFCC" /><a href="homework3pt4.html">Part 4</a></td>
    <td align = "center"><font face ="baskerville" color="#FFFFCC" /><a href="homework3pt5.html">Part 5</a></td>
    <td align = "center"><font face ="baskerville" color="#FFFFCC" /><a href="homework3pt6.html">Part 6</a></td></tr></table>

<br />
<div class="creditbox" align="center" >
<form name="creditLimitForm" action="">
<br />
<h1 class="header">Customer Credit Limit Check</h1>
<br />


<table class="creditLimitForm">
<tr>
<td>Customer Account Number:</td>
<td><input type="text" id="accountNum" name="accountNum" size="15" /></td>
</tr>
<tr>
<td>1st of Month Balance: </td>
<td>$<input type="text" id="beginningBalance" name="beginningBalance" size="15" /></td></tr>
<tr>
<td>Total charged by customer this month: </td>
<td>$<input type="text" id="amtCharged" name="amtCharged" size="15" /></td>
</tr>
<tr>
<td>Total credits applied to account </td>
<td>$<input type="text" id="creditsApplied" name="creditsApplied" size="15" /></td>
</tr>
<tr>
<td>Allowed Credit Limit</td>
<td>$<input type="text" id= "creditLimit" name="creditLimit" size="15" /></td>
</tr>
</table>

<br />
<textarea rows="7" cols="50" name="creditDisplay"></textarea>
<br /><br />
<input type="button" onclick="creditCalc();" value="Check Credit" />&nbsp;&nbsp;&nbsp;&nbsp;

<input type="reset" value="Reset Form" />

<p>Credit Available: </p>
<p id="creditAvailable"></p>

</form>
<br />
<br />
</div>
</body>
</html>

任何可以来救援的优秀调试器?

4

4 回答 4

0

使用 Firebug for Firefox 或 Chrome 的内置调试工具(或其他浏览器的类似工具),它会让您的生活更轻松...

至于你的具体问题,onclick确实应该是调用函数,所以错误一定在creditCalc函数的某个地方。如果您仍然遇到问题,请至少转到浏览器中的错误控制台并在此处发布输出;如果没有任何错误消息,则很难猜测问题可能是什么。

编辑:错误控制台中也可能没有任何错误,这对回答您的问题的人来说也是有用的信息。

于 2013-02-25T05:19:31.287 回答
0

在 chrome 上很容易找到 javascript 中的错误。
您只需在 chrome 中加载或重新加载页面之前单击鼠标右键。并选择

inspect element

比在底部工具栏上。查找Console单击它,您将能够在文件中看到任何 javascript 错误。

于 2013-02-25T05:29:29.360 回答
0

好的,这里试试这段代码:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title>Homework 3 Part 3</title>
<style type="text/css">
tr.visiting {background-color: #000000; }


h1.header {color: #333333;} 
.creditbox { 

    background-color: #ffcc66;
    width: 60%;
    border:solid 5px #666666;
    margin-left:auto;
margin-right:auto;

}

table {
    text-align: right;
}
td.visiting {background-color: #000000; }
input {text-align: right;}
</style>
<script type="text/javascript">
/* <![CDATA[ */

function creditCalc() 
{
var account, credLim, begBalance, totalCharged, credit, balance, endBalance;

    account = parseInt(document.creditLimitForm.accountNum.value);
    credLim = parseInt(document.creditLimitForm.creditLimit.value);
    begBalance = parseInt(document.creditLimitForm.beginningBalance.value);
    totalCharged = parseInt(document.creditLimitForm.amtCharged.value);
    credit = parseInt(document.creditLimitForm.creditsApplied.value);   

    balance = begBalance + totalCharged;
    endBalance = balance - credit;
/*
    documemt.creditLimitForm.creditDisplay.value("Account Number:  " + account + "\nCredit Limit:  " + credLim + "\nBeginning Balance: " + begBalance + "\nTotal Charged this month:  " + totalCharged + "\nTotal credits this month:  " + credit + "\nCredit Available:  " + endBalance);*/

document.getElementById("creditAvailable").innerHTML = "Account Number:  " + account + "\nCredit Limit:  " + credLim + "\nBeginning Balance: " + begBalance + "\nTotal Charged this month:  " + totalCharged + "\nTotal credits this month:  " + credit + "\nCredit Available:  " + endBalance;

}

/*]]>*/
</script>

</head>

<body bgcolor = "#ffffff" alink = "#FFFFCC" link = "#FFFFCC" vlink = "#FFFFCC">
<table id="border" width="70%" align ="center" cellpadding="0">
        <tr>
        <td align="center">
            <img src="hw3Banner.jpg" alt="Homework Banner" align="middle" />
        </td>
        </tr>
    </table>
    <br />
    <table border="1" width="500" align ="center" bgcolor="#666666" cellpadding="2.5">
     <tr><td align = "center"><font face ="baskerville" color="#FFFFCC" /><a href="homework3.html">HW 3 Main</a></td>
     <td align = "center"><font face ="baskerville" color="#FFFFCC" /><a href="homework3pt1.html">Part 1</a></td>
     <td align = "center"><font face ="baskerville" color="#FFFFCC" /><a href="homework3pt2.html">Part 2</td>
     <td class="visiting" align = "center"><font face ="baskerville" color="#FFFFCC" /><a href="homework3pt3.html">Part 3</a></td>
    <td align = "center"><font face ="baskerville" color="#FFFFCC" /><a href="homework3pt4.html">Part 4</a></td>
    <td align = "center"><font face ="baskerville" color="#FFFFCC" /><a href="homework3pt5.html">Part 5</a></td>
    <td align = "center"><font face ="baskerville" color="#FFFFCC" /><a href="homework3pt6.html">Part 6</a></td></tr></table>

<br />
<div class="creditbox" align="center" >
<form name="creditLimitForm" action="">
<br />
<h1 class="header">Customer Credit Limit Check</h1>
<br />


<table class="creditLimitForm" id="creditLimitForm">
<tr>
<td>Customer Account Number:</td>
<td><input type="text" id="accountNum" name="accountNum" size="15" /></td>
</tr>
<tr>
<td>1st of Month Balance: </td>
<td>$<input type="text" id="beginningBalance" name="beginningBalance" size="15" /></td></tr>
<tr>
<td>Total charged by customer this month: </td>
<td>$<input type="text" id="amtCharged" name="amtCharged" size="15" /></td>
</tr>
<tr>
<td>Total credits applied to account </td>
<td>$<input type="text" id="creditsApplied" name="creditsApplied" size="15" /></td>
</tr>
<tr>
<td>Allowed Credit Limit</td>
<td>$<input type="text" id= "creditLimit" name="creditLimit" size="15" /></td>
</tr>
</table>

<br />
<textarea rows="7" cols="50" name="creditDisplay"></textarea>
<br /><br />
<input type="button" onclick="creditCalc();" value="Check Credit" />&nbsp;&nbsp;&nbsp;&nbsp;

<input type="reset" value="Reset Form" />

<p>Credit Available: </p>
<p id="creditAvailable"></p>

</form>
<br />
<br />
</div>
</body>
</html>
于 2013-02-25T05:36:06.170 回答
0
  the problem with your this line otherwise its working fine.just try with alert instead.

     documemt.creditLimitForm.creditDisplay.value("Account Number:  " + account + "\nCredit    Limit:  " + credLim + "\nBeginning Balance: " + begBalance + "\nTotal Charged this month:  " + totalCharged + "\nTotal credits this month:  " + credit + "\nCredit Available:  " + endBalance);

这是造成问题。

documemt.creditLimitForm.creditDisplay.value

尝试警报并检查。

于 2013-02-25T05:36:28.170 回答