gpt4 book ai didi

javascript - 无需刷新页面即可重置功能

转载 作者:行者123 更新时间:2023-12-03 03:05:05 25 4
gpt4 key购买 nike

所以我在玩一些数学函数时,遇到了一个问题。我有一些“onClick”输入,但我的问题是:我可以计算一次,但如果再试一次,就不行了。然后我必须刷新页面才能尝试使用新值。有人知道如何解决这个问题吗?

<!doctype html>
<html>
<head>
</head>
<body>
<h1>Quadratic equation Calculator</h1>
</br>
</br>
<p>Select A value</p> <input id="a"></input>
<p>Select B value</p> <input id="b"></input>
<p>Select C value</p> <input id="c"></input></br>
<button class="knapp" onClick="a();b();c();abc(a,b,c)">Calculate</button>
<button class="knapp" onClick="location.href=location.href">Refresh</button>

<h1 id="svar"></h1>
<h1 id="svar2"></h1>


<script>

function a() {
a = document.getElementById("a").value;
return a;
}
function b() {
b = document.getElementById("b").value;
return b;
}
function c() {
c = document.getElementById("c").value;
return c;
}




function abc(a,b,c) {

var minB = -b;
var underRot = Math.pow(b,2)-(4*a*c);
var rot = Math.sqrt(underRot);
var formelPluss = (minB + rot)/(2*a);
var formelMinus = (minB - rot)/(2*a);

document.getElementById("svar").innerHTML = "X1 = " + formelPluss;
document.getElementById("svar2").innerHTML = "X2 = " + formelMinus;

}

</script>





</body>
</html>

最佳答案

您需要使用 let 将它们设置为变量。

<!doctype html>
<html>
<head>
</head>
<body>
<h1>Quadratic equation Calculator</h1>
</br>
</br>
<p>Select A value</p> <input id="a"></input>
<p>Select B value</p> <input id="b"></input>
<p>Select C value</p> <input id="c"></input></br>
<button class="knapp" onClick="a();b();c();abc(a,b,c)">Calculate</button>
<button class="knapp" onClick="location.href=location.href">Refresh</button>

<h1 id="svar"></h1>
<h1 id="svar2"></h1>


<script>

function a() {
let a = document.getElementById("a").value;
return a;
}
function b() {
let b = document.getElementById("b").value;
return b;
}
function c() {
let c = document.getElementById("c").value;
return c;
}




function abc(a,b,c) {

var minB = -b;
var underRot = Math.pow(b,2)-(4*a*c);
var rot = Math.sqrt(underRot);
var formelPluss = (minB + rot)/(2*a);
var formelMinus = (minB - rot)/(2*a);

document.getElementById("svar").innerHTML = "X1 = " + formelPluss;
document.getElementById("svar2").innerHTML = "X2 = " + formelMinus;

}

</script>





</body>
</html>

关于javascript - 无需刷新页面即可重置功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47201327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com