<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title></title> </head> <body> <script type="text/javascript"> function getPrice() { alert("getPrice()"); var prices =document.forms["tt"]["price"].value var prices =document.getElementById("price").value; var parr2=prices.split(","); var sum = 0; var len =parr2.length; for(var i=0;i<len;i++){ sum += parseInt(parr2[i]);} document.forms["tt"]["sum"].value=sum; } </script> <tr> <td>请输入5本书的价格,用逗号隔开</td> <br><br><br> <td><input type="text" id="price"/></td> <td><input type="button" value="总计" οnclick="getPrice()"/></td> <td><input type="text" name="sum" value=""/></td> </tr> <!--. 输入表单,输入五本书价格,自动计算价格总和 .--> <!--. <tr> <td>输入书本价格:</td> <td><input type="text" name="price" value=""/></td> <td><input id="" type="button" name="submit" value="和"/></td> <td><input type="text" name="sum" value=""/></td> </tr>.--> </body> </html>
时间: 2023-05-28 16:02:13 浏览: 132
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>书本价格总和计算器</title>
</head>
<body>
<script type="text/javascript">
function getPrice() {
var prices = document.getElementById("price").value; // 获取输入框中的价格值
var parr2 = prices.split(","); // 将字符串按逗号拆分为数组
var sum = 0;
var len = parr2.length;
for (var i = 0; i < len; i++) {
sum += parseFloat(parr2[i]); // 将字符串转为浮点数并累加
}
document.getElementById("sum").value = sum.toFixed(2); // 将计算结果写入总价输入框
}
</script>
<table>
<tr>
<td>请输入5本书的价格,用逗号隔开:</td>
<td><input type="text" id="price"/></td>
<td><input type="button" value="总计" onclick="getPrice()"/></td>
<td><input type="text" id="sum"/></td>
</tr>
</table>
</body>
</html>
阅读全文