網頁Web-Javascript-表單-使用者輸入單價、數量後,計算出總額

  • 10092
  • 0
  • Web
  • 2023-07-13

文、意如

使用者輸入單價、數量後,計算出總額

#提示:
//抓值
xx = document.getElementById("xx").value;
//賦予值
document.getElementById("aa").value = totals;

page.html
 

<html>
<head>
</head>
<body>
	單價<input type="text" id="price" name="price"><br>
	數量<input type="number" min="0" max="27" id="num"><br>
	<button onclick="cal()">計算</button><br>
		
	總價<input type="text" id="total">
</body>
</html>
<script>
			
function cal() {
	pirce = document.getElementById("price").value;
	num = document.getElementById("num").value;
	totals=pirce*num;
	document.getElementById("total").value = totals;
}
</script>

參考

https://codepen.io/yiruatstudio/pen/qBNKreJ

#Javascript-在function()中再呼叫其他function()

<html>
<head> </head>

<body>

A值<input type="text" id="priceA" name="price"><br>
B值<input type="text" id="priceB" name="price"><br>

<button onclick="cal()">計算</button><br>

總價<input type="text" id="total">

</body>
</html>
<script>

function cal() {
	pirceA = document.getElementById("priceA").value;
	pirceB = document.getElementById("priceB").value;

	//呼叫cal2(a,b)
	num = cal2(pirceA,pirceB);
	document.getElementById("total").value = num;
}

function cal2(a,b){
	return a*b;
}
</script>

參考
 


 

 

Yiru@Studio - 關於我 - 意如