2015-05-06 65 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; 
}