繁体   English   中英

HTML输入和Javascript不返回值

[英]HTML input and Javascript not returning values

我决定作为编码练习来制作一个假设计算器。 效果很好,但是我坚持将我的Javascript代码实现为HTML。 这是我的代码:

<!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按钮之前正在调用该函数。

所以,现在的字符串是不会改变 我希望我的问题足够具体。

 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);

有回报c; 声明,因此答案将不会更新。 str(c)定义str()的位置。 这是适合您的工作代码。

我们可以做一个额外的验证,只输入数字且大于零,将其写入以获得更好的性能。

变量ab必须在其他用户提到的全局范围内。 否则,它们是undefined

在您的程序中,这些值是函数的局部值,因此,这些值在相应函数的局部范围内,并且在相应函数外部不可访问/可见。 通过在全局范围内声明它们,您可以在您定义的任何其他子范围(例如函数)中访问这些变量。

同样,删除hypo函数中的参数ab 它们不是必需的,并且可能会干扰全局范围中的变量ab的值,因为具有相同名称的局部范围变量会覆盖父级范围的变量。

还要确保不要return c ,因为该函数将返回c的值并结束,并且不会按需要将结果写入HTML。 我认为您不需要return语句。

它们是未定义的,因为它们应作为参数使用。 因此,您应该将它们设置为全局或以其他方式将其作为参数传递。 2.您的变量包含在其函数中。 您需要使它们成为全局属性以从其他功能访问

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

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

我强烈建议您查看javascript中变量的范围是什么

注释后编辑:您的“斜边”功能应如下所示:

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>

您声明的ab变量在函数范围之外不可见。

并且不要使用全局变量,这会弄乱代码并滥用javascript。

您为什么不创建表格呢? 还是您真的想玩提示? 您可以添加两个输入(A和B),然后单击“提交”即可更改结果。

正如@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>
<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>

泰勒你好,这是给你的工作代码,它将为你显示结果

注意:return c不会返回结果ID中的值。 它将返回函数外部的值,而不是in函数。

str():仅适用于javascript中的某些索引,例如str.search(“ value”)。 因此它是不确定的。

另外,如果要动态打印结果,则需要在单击按钮后保存a和b的值,调用函数时也需要调用参数。

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

就我而言,我将静态学习12,13,

console.log(a或b)获取a的值,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>

函数内部使用的变量未全局声明。

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM