最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

js實(shí)現(xiàn)ATM機(jī)存取款功能

 更新時(shí)間:2021年04月19日 14:34:53   作者:wy__kobe  
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)ATM機(jī)存取款功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

js是一個(gè)功能十分強(qiáng)大的腳本語(yǔ)言,通過(guò)js能實(shí)現(xiàn)很多有意思的demo!而要實(shí)現(xiàn)那些功能炫酷、特效美觀的東西DOM操作是必不可少且尤為重要的!這個(gè)ATM機(jī)存取款的案例,就用到j(luò)s中一些簡(jiǎn)單的DOM操作來(lái)實(shí)現(xiàn)其功能。

ATM機(jī)案例功能需求:

1.用戶最多只能有三次輸入卡號(hào)和密碼的機(jī)會(huì),如果超過(guò)三次,則提示卡號(hào)已被鎖定
2.用戶取款的金額不能大于卡上的賬戶余額
3.存取功能完成后,要更新相應(yīng)的余額信息 

登錄界面代碼:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>ATM</title>
 <script src="ATM.js"></script>
 <style>
 div{
 width: 300px;
 height: 200px;
 margin: 0 auto;
 border:1px solid black;
 border-radius: 5px;
 text-align: center;
 }
 p {
 font-size: 20px;
 }
 button {
 border: 0px;
 padding: 5px;
 background-color: green;
 color: white;
 }
 </style>
</head>
<body>
 <div>
 <p>ATM機(jī)</p>
 <p><label>卡號(hào):</label><input type="text" id="account"></p>
 <p><label>密碼:</label><input type="password" id="password"></p>
 <p><button onclick="login()">登錄</button></p>
 </div>
</body>
</html>

主頁(yè)界面代碼:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>ATM</title>
 <script src="ATM.js"></script>
 <style>
 div{
 width: 300px;
 height: 200px;
 margin: 0 auto;
 border:1px solid black;
 border-radius: 5px;
 text-align: center;
 }
 p {
 font-size: 20px;
 }
 button {
 border: 0px;
 padding: 5px;
 background-color: green;
 color: white;
 }
 </style>
</head>
<body>
 <div>
 <p>ATM機(jī)</p>
 <p><label>余額:</label><input type="text" id="balance" value="2000.00" disabled></p>
 <p><label>存款:</label><input type="text" id="deposit">&nbsp;<button onclick="deposit()">存款</button></p>
 <p><label>取款:</label><input type="text" id="withdraw">&nbsp;<button onclick="withdraw()">取款</button></p>
 
 </div>
</body>
</html>

js代碼:

var i = 2; //定義密碼輸錯(cuò)的次數(shù)

//判斷輸入的卡號(hào)是不是數(shù)字類型
//返回true,證明不是數(shù)字;返回false,證明是數(shù)字

 function checkNumber(account){
 var pattern=/^[0-9]*[1-9][0-9]*$/;
 return pattern.test(account);
 // return isNaN(account);
 }

//判斷輸入的卡號(hào)和密碼是否為空
 function checkNull(account,password){
 if(account.length>0 && password.length>0){
 return true;
 }
 return false;
 }

//登錄事件
 function login(){
 var account=document.getElementById("account").value;
 var password=document.getElementById("password").value;

 if(!checkNull(account,password)){
 alert("卡號(hào)和密碼不能為空!");
 return; //終止登錄方法,下面的代碼不會(huì)執(zhí)行
 }
 if(!checkNumber(account)){
 alert("卡號(hào)必須為數(shù)字!");
 return;
 }
 if(i>0 && account=="123456789" && password=="123"){
 window.location.href="index.html" rel="external nofollow" ;
 }else{
 if(i == 0){
 alert("當(dāng)前卡號(hào)被鎖定!");
 return;
 }
 alert("你還剩下"+i+"次輸入卡號(hào)和密碼的機(jī)會(huì)");
 i--;
 return;
 }
 }

//存款
 function deposit(){
 var balance = parseFloat(document.getElementById("balance").value); //獲取余額,并將其轉(zhuǎn)換為數(shù)字
 var deposit = document.getElementById("deposit").value;

 if(!deposit.length>0){
 alert("請(qǐng)輸入您要存款的金額");
 return;
 }
 if(checkNumber(deposit)){
 alert("請(qǐng)輸入數(shù)字");
 return;
 }

 balance+=parseFloat(deposit);
 document.getElementById("balance").value=balance; //修改存款完成以后顯示的余額

 }

 //取款
 function withdraw(){
 var balance = parseFloat(document.getElementById("balance").value); //獲取余額,并將其轉(zhuǎn)換為數(shù)字
 var withdraw = document.getElementById("withdraw").value;

 if(!withdraw.length>0){
 alert("請(qǐng)輸入您要取款的金額");
 return;
 }
 if(checkNumber(withdraw)){
 alert("請(qǐng)輸入數(shù)字");
 return;
 }

 //判斷取款是否大于余額
 if(parseFloat(withdraw)>balance){
 alert("余額不足!");
 }

 balance-=parseFloat(withdraw);
 document.getElementById("balance").value=balance;
 }

運(yùn)行效果:




以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關(guān)文章

最新評(píng)論

边坝县| 瑞金市| 堆龙德庆县| 安溪县| 洪雅县| 永年县| 溆浦县| 临洮县| 兰西县| 石门县| 黑河市| 万安县| 堆龙德庆县| 武宁县| 区。| 南开区| 肇东市| 皋兰县| 吐鲁番市| 汤阴县| 元阳县| 乌什县| 鲜城| 元谋县| 上栗县| 朝阳县| 慈利县| 阿鲁科尔沁旗| 贵州省| 曲麻莱县| 日土县| 崇左市| 漳平市| 龙陵县| 南澳县| 余干县| 青河县| 富阳市| 平泉县| 浏阳市| 连平县|