2015-05-06 63 views
1

我決定作爲編碼練習來製作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問題。但是在做了一些關於網站的研究之後,我做了一些調整。最初,變量ab在一個稱爲getData的函數中,變量c等於函數hypothenuse。我移動了變量c,因爲它在按下Calculate按鈕之前調用了該函數。

所以現在字符串不會改變根本就是。我希望我的問題足夠具體。

+0

嘗試解析提示 – Alpha2k

回答

0
<html> 
<head> 
<script> 
var a; 
var b; 
var c; 

function getValueA() { 
    a = prompt("What is value a?"); 
    } 

function getValueB() { 
    b = prompt("What is value b?"); 
} 

function hypothenuse() { 

    c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)); 
    //return c 
document.getElementById("result").innerHTML = "Answer is:" + 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> 
+0

謝謝。這工作。對於有同樣問題的人來說,被改變的東西是:'str()'被刪除,''返回'被從函數中刪除,'var a','var b'和'var c'被添加在頂端。 – Tyler

0

。它們是未定義的,因爲它們被期望作爲參數。所以你應該讓它們成爲全局的或者以其他方式作爲參數傳遞它們。你的變量被包含在它們的函數中。你需要讓他們的全球從另一個功能

var a; 
var b; 
function getValueA() { 
    a = prompt("What is value a?"); 
} 

function getValueB() { 
    b = prompt("What is value b?"); 
} 

我強烈建議訪問你看到評論後What is the scope of variables in javascript

編輯: 你「hypothenuse」功能應該是這個樣子:

function hypothenuse() { 
    // Notice you don't need parameters if you use global variables 
    var c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)); 
    document.getElementById("result").innerHTML = "Answer is:" + c; 
    // who is str(c) in your case? 
    return c; 
} 

如果您首先返回變量c,則該函數停止,並且通過更改字符串的最後一部分不會運行。所以應該改變字符串第一個,然後返回

或者你可以嘗試其他出頭:

<!DOCTYPE html> 
<html> 
<head> 
<script> 

function hypothenuse() { 
    var a = prompt("a = "); 
    var b = prompt("b = "); 
    var c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)); 
    document.getElementById("result").innerHTML = "Answer is:" + c; 
} 

</script> 
</head> 

<body> 

<p id="result">Answer:</p> 

<button type="submit" onclick="hypothenuse()">Calculate</button> 

</body> 
</html> 
+0

謝謝,但按下計算按鈕後,字符串不會改變。我在函數中加入了'''來確保當我按下按鈕時某些東西正在改變。 – Tyler

+0

編輯我的回答 –

0

ab變量您聲明是不可見的外部功能範疇。

不要使用全局變量,你搞亂了代碼並濫用JavaScript。

你爲什麼不創建一個表單?或者你真的想玩提示嗎? 您可以添加兩個輸入(A和B),當您單擊提交時,您可以更改結果。

+0

是的,我剛剛編輯問題,並問我如何使用數字輸入來更改JavaScript變量。 – Tyler

0

@kikyalex指出,問題在於您的變量在全局範圍內不可用。你需要這樣的解決方案。

<!DOCTYPE html> 
<html> 
<head> 
<script> 
var a, b 
function getValueA() { 
    a = prompt("What is value a?"); 
} 

function getValueB() { 
    b = prompt("What is value b?"); 
} 

function hypothenuse() { 
    var c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)); 
    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> 
+0

如果您想修改「結果」,則必須刪除「return c」語句。 – Elyasin

1

var a,b; 
 
function getValueA() { 
 
    a = prompt("What is value a?"); 
 
} 
 

 
function getValueB() { 
 
    b = prompt("What is value b?"); 
 
} 
 

 
function hypothenuse(a, b) { 
 
    var c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)); 
 
    document.getElementById("result").innerHTML = "Answer is:" + (c); 
 
}
<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(a, b)">Calculate</button>

有幾件事情,我們需要重點關注, 而

document.getElementById("result").innerHTML = "Answer is:" + (c); 

有回報℃;聲明如此回答將不會更新。 str(c)你在哪裏定義了str()。 這裏是你的工作代碼。

而且我們可以對輸入數字和大於零的數字進行額外的驗證。寫出這些以獲得更好的性能。

1

變量ab需要在全球範圍內的其他用戶提及。否則它們是undefined

在您的程序中,這些值是函數的本地值,因此這些值位於相應函數的本地範圍內,並且在相應函數之外不可訪問/可見。通過在全局範圍中聲明它們,您可以訪問您定義的任何其他子範圍(例如,函數)中的這些變量。

此外,刪除hypo函數中的參數ab。它們不是必需的,可能會干擾全局範圍內的變量ab的值,因爲具有相同名稱的本地範圍變量會覆蓋父級範圍內的變量。

也請確保不要return c,因爲該函數將返回c的值並結束,並且不會按照需要將結果寫入HTML。我認爲你不需要返回聲明。

0

您好泰勒這裏是爲你工作的代碼,這將導致diplay爲你

注:回覆C不會導致ID返回值。它將返回函數外的值而不是函數中的值。 str():只會在javascript中的某些索引(如str.search(「value」))下工作。所以這是不確定的。

此外,如果你想打印結果動態,那麼你需要按鈕點擊後保存a和b的值,當調用函數時也需要調用參數。

<button type="submit" onclick="hypothenuse(a,b)">Calculate</button> 

在我來說,我將帶12,13靜態,

的console.log(A或B)得到的值和b打印動態產生。

<!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) { 
console.log(a); 
var c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)); 
document.getElementById("result").innerHTML = "Answer is:" + 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(12,13)">Calculate</button> 

</body> 
</html> 
0

該函數內使用的變量沒有全局聲明。

var a, b, c; 

function getValueA() { 
a = prompt("What is value a?"); 
} 

function getValueB() {  
b = prompt("What is value b?"); 
} 

function hypothenuse() 
{ 
var c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)); 
return c; 
document.getElementById("result").innerHTML = "Answer is:" + c; 
}