我是新來的java腳本,我有三個文本字段與ID分別爲文本1,文本2,文本3。我想輸入其中2個值並在第三個中輸出總和。如何在Java腳本函數中傳遞文本字段值?
我的代碼看起來像這樣,請告訴我,我做錯了什麼。 它將它們添加爲字符串而不是數字。
另外我想說,如果我在三個框中的任意兩個中輸入值。另一個調整自己。 EX:'__'+ 5 = 7 =>'2'+ 5 = 7
如果我將變量放在值屬性中,它會起作用。如果那麼那麼如何?
<html>
<head>
<script>
function myCalculator(a, b) {
c = a + b;
document.getElementById("text3").value = c;
}
</script>
</head>
<body>
<p>
<h1>Calculator</h1>
<input type="text" value="" id="text1"></input> + <input type="text" value="" id="text2"></input> = <input type="text" value="" id="text3"></input>
<input type="button" value="ADD" onclick='myCalculator(document.getElementById("text1").value,document.getElementById("text2").value)'></input>
</p>
</body>
</html>
你可能想看看[parseFloat(https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/parseFloat),根據您的要求。 – Xotic750