创建一个简单的ATM程序

时间:2014-03-14 21:48:14

标签: javascript function

我正在尝试使用5个按钮($ 20,$ 60,$ 100,其他金额和提款)创建一个简单的ATM程序。我需要在按下金钱按钮时创建一个确认框,并在按下撤销按钮时从文本框中扣除金额。这是我的代码:

<form action="" >
    <table>
    <tr>
    <td>
        <input type="button" value="$20" onclick='accept())'/>
    </td>
    <td>

    </td>
    <td>

    </td>
    <td>
        <input type="button" value="Withdrawal" onclick=''/>
    </td>
    </tr>
    <tr>
        <td>
            <input type="button" value="$60" onclick='accept()'/>
        </td>
        <td>Current Balance

        </td>
        <td>
            <input type="Current Balance" value="5000.00" onchange=''/>
        </td>
        <td>

        </td>
    </tr>
    <tr>
        <td>
            <input type="button" value="$100" onclick='accept()'/>
        </td>
        <td>

        </td>
        <td>

        </td>
        <td>

        </td>
    </tr>
    <tr>
        <td>
            <input type="button" value="Other Amount" onclick=''/>
        </td>
        <td>

        </td>
        <td>

        </td>
        <td>

        </td>
    </tr>
    </table>
   </form> 

<script type="text/javascript">

var balance=50000;  

function withdraw(amount)       
{

var sure = confirm("Are You Sure You Want To Withdraw This Amount?");
if(true)
{
balance = balance - amount;
}else
{
alert("No Withdrawal Made");
}
alert("Your Balance Is "+balance);

function 


</script>

请帮忙!这真让我抓狂。谢谢!

2 个答案:

答案 0 :(得分:0)

我没有清楚地得到问题,但我认为您需要确认您的ATM应用程序的“其他金额”按钮的金额。你假设使用“提示”,使用它就像一个警告框。

<input type="button" value="Get" onclick="prompt('How Much Amount, you want to withdraw?')">

您的代码可以是某种东西,如上所示。

答案 1 :(得分:0)

好让我们看看一些问题;

  • 正确缩进代码,它将帮助您更轻松地发现错误。
  •  
  • 使用像notepad ++这样的编辑器或更好的IDE,它将显示您的语法错误
  • 不要在标记中使用表格进行布局,请参阅css
  • <form action="" >
    <table>
    <tr>
    <td>
        <input type="button" value="$20" onclick='accept())'/> // you have an extra closing brace here
    </td>
    <td>
    
    </td>
    <td>
    
    </td>
    <td>
        <input type="button" value="Withdrawal" onclick=''/>
    </td>
    </tr>
    <tr>
        <td>
            <input type="button" value="$60" onclick='accept()'/>
        </td>
        <td>Current Balance
    
        </td>
        <td>
            <input type="Current Balance" value="5000.00" onchange=''/> //Current balance is not a valid type
        </td>
        <td>
    
        </td>
    </tr>
    <tr>
        <td>
            <input type="button" value="$100" onclick='accept()'/>
        </td>
        <td>
    
        </td>
        <td>
    
        </td>
        <td>
    
        </td>
    </tr>
    <tr>
        <td>
            <input type="button" value="Other Amount" onclick=''/>
        </td>
        <td>
    
        </td>
        <td>
    
        </td>
        <td>
    
        </td>
    </tr>
    </table>
    

    <script type="text/javascript">
    
    var balance=50000;  
    
    function withdraw(amount)   //this function should be called accept not withdraw 
     { //you didn't close this brace
    
      var sure = confirm("Are You Sure You Want To Withdraw This Amount?");
      if(true)  //should be if (sure)
          {
            balance = balance - amount;
          }else{
            alert("No Withdrawal Made");
          }
           alert("Your Balance Is "+balance);
    
         function  //this shouldn't be here
    
    
    </script>