我決定作爲編碼練習來製作hyphenuse計算器。它運行良好,但我堅持實施我的Javascript代碼到HTML。這裏是我的代碼:HTML輸入和Javascript沒有返回值
<!DOCTYPE html>
<html>
<head>
<script>
function getValueA() {
var a = prompt("What is value a?");
}
function getValueB() {
var b = prompt("What is value b?");
}
function hypothenuse(a, b) {
var c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
return c
document.getElementById("result").innerHTML = "Answer is:" + str(c);
}
</script>
</head>
<body>
<p id="result">Answer:</p>
<button type="button" onclick="getValueA()">Input Value a</button>
<button type="button" onclick="getValueB()">Input Value b</button>
<button type="submit" onclick="hypothenuse()">Calculate</button>
</body>
</html>
使用來是返回的字符串是undefined
問題。但是在做了一些關於網站的研究之後,我做了一些調整。最初,變量a
和b
在一個稱爲getData
的函數中,變量c
等於函數hypothenuse
。我移動了變量c
,因爲它在按下Calculate
按鈕之前調用了該函數。
所以現在字符串不會改變根本就是。我希望我的問題足夠具體。
嘗試解析提示 – Alpha2k