javascript初学者做得一个简单计算器!
<!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">
<head>
<title>拍拍购物车</title>
<style type="text/css">
</style>
<script type="text/javascript">
//价格
var number = 100;
//商品合计
var numberMoney;
//总合计
var sumMoney;
function radioValue(){
var num = document.getElementById("numNumber").value;
//匹配正整数正则表达式
var numRegExp =/^[1-9]\d*$/;
if(num == ""){
confirm("请先输入购买的数量!!!");
return false;
}
if(!numRegExp.test(num)){
alert("购买的数量为正整数!!!");
return false;
}
if(num>52){
//alert("超过库存数量");
document.getElementById("message").innerHTML="最大限购量52件";
document.getElementById("message").style.color="red";
num=52;
document.getElementById("numNumber").value = num;
}
var obj = document.getElementsByName("radioName");
var moneySMN;
for(var i=0;i<obj.length;i++){//适合length>=2时,当obj.length==null时,可以直接取obj.value值
if(obj[i].checked){
moneySMN=obj[i].value;
break;
}
}
//商品总价格
numberMoney = parseInt(num) * number;
document.getElementById("numberMoney").value = numberMoney;
sumMoney = numberMoney + parseInt(moneySMN);
document.getElementById("sumMoney").value = sumMoney;
}
/*
document.getElementById(id).style.borderWidth="1px";
document.getElementById(id).style.width="400px";
document.getElementById(id).style.borderColor="#FF0000";
document.getElementById(id).style.borderStyle="solid";
document.getElementById(id).style.backgroundColor="99FFDD";
*/
</script>
</head>
<body>
邮寄:
<input type="radio" name="radioName" value="50.00" onclick="radioValue()"/>50.00
MSD:
<input type="radio" name="radioName" value="80.00" checked="checked" onclick="radioValue()"/>80.00</br>
数量:<input type="text" id="numNumber" name="number" onblur="radioValue()" /> <span id="message"></span></br>
商品合计:
<input type="text" id="numberMoney" name="numberMoney" readonly="readonly"/></br>
应付总价:
<input type="text" id="sumMoney" name="sumMoney" readonly="readonly"/>
</body>
</html>