js實(shí)現(xiàn)ATM機(jī)存取款功能
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"> <button onclick="deposit()">存款</button></p>
<p><label>取款:</label><input type="text" id="withdraw"> <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)文章
理解Javascript_15_作用域分配與變量訪問(wèn)規(guī)則,再送個(gè)閉包
在閱讀本博文之前,請(qǐng)先閱讀《理解Javascript_13_執(zhí)行模型詳解》 在'執(zhí)行模型詳解'中講到了關(guān)于作用域分配的問(wèn)題,這一篇博文將詳細(xì)的說(shuō)明函數(shù)對(duì)象、作用域鏈與執(zhí)行上下文的關(guān)系。2010-10-10
JavaScript+CSS實(shí)現(xiàn)仿天貓側(cè)邊網(wǎng)頁(yè)菜單效果
這篇文章主要介紹了JavaScript+CSS實(shí)現(xiàn)仿天貓側(cè)邊網(wǎng)頁(yè)菜單效果,涉及javascript鼠標(biāo)事件及頁(yè)面元素動(dòng)態(tài)操作的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
JS使用Expires?max-age判斷緩存過(guò)期的瀏覽器實(shí)例
這篇文章主要為大家介紹了JS使用Expires?max-age判斷緩存過(guò)期的瀏覽器實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
fetch 如何實(shí)現(xiàn)請(qǐng)求數(shù)據(jù)
這篇文章主要介紹了fetch 如何實(shí)現(xiàn)請(qǐng)求數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
js點(diǎn)擊頁(yè)面其它地方將某個(gè)顯示的DIV隱藏
今天一朋友問(wèn)我 點(diǎn)擊一按鈕彈出一個(gè)DIV,然后要求點(diǎn)擊頁(yè)面其它地方隱藏這個(gè)DIV2012-07-07
《JavaScript DOM 編程藝術(shù)》讀書(shū)筆記之JavaScript 圖片庫(kù)
這篇文章主要介紹了《JavaScript DOM 編程藝術(shù)》讀書(shū)筆記之JavaScript 圖片庫(kù),需要的朋友可以參考下2015-01-01
JavaScript正則替換HTML標(biāo)簽功能示例
這篇文章主要介紹了JavaScript正則替換HTML標(biāo)簽功能,結(jié)合完整實(shí)例形式詳細(xì)分析了javascript正則替換字符串操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-03-03

