gpt4 book ai didi

javascript 函数可能由于范围问题而未调用

转载 作者:行者123 更新时间:2023-11-30 23:43:22 24 4
gpt4 key购买 nike

我希望调用 clear() JS 函数来清除网页上某些输入字段中的文本值,但由于某种原因,该函数似乎未正确调用。我相信这可能是一个范围问题,但我不确定。

这是我的标记、脚本和样式,目前将其放在一起以便于使用,但一旦脚本完成,将进行修改:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Quadratic Root Finder</title>
<script>
function calculateQuad()
{
var inputa = document.getElementById('variablea').value;
var inputb = document.getElementById('variableb').value;
var inputc = document.getElementById('variablec').value;

root = Math.pow(inputb,2) - 4 * inputa * inputc;
root1 = (-inputb + Math.sqrt(root))/2*inputa
root2 = (-inputb + Math.sqrt(root))/2*inputa

document.getElementById('root1').value = root1;
document.getElementById('root2').value = root2;
if(root<'0')
{
alert('This equation has no real solution.')
}
else {
if(root=='0')
{
document.getElementById('root1').value = root1
document.getElementById('root2').value = 'No Second Answer'
}
else {
document.getElementById('root1').value = root1
document.getElementById('root2').value = root1
}
}
}
function clear()
{
document.getElementById('variablea').value = "";
document.getElementById('variableb').value = "";
document.getElementById('variablec').value = "";
}
</script>
<style>
#container
{
text-align: center;
}
</style>
</head>

<body>
<div id="container">
<h1>Quadratic Root Finder!</h1>
<form id="form1">
a:<input id="variablea" value="" type="text">
<br/>
b:<input id="variableb" value="" type="text">
<br />
c:<input id="variablec" value="" type="text">
<br />
<input id="calculate" value="Calculate!" type="button" onClick="calculateQuad()">
<input id="erase" value="Clear" type="button" onClick="clear()">
<br />
<br />
Roots:
<br />
<input id="root1" type="text" readonly>
<br />
<input id="root2" type="text" readonly>
</form>
</div>
</body>
</html>

最佳答案

您必须重命名“clear”方法。尝试将其命名为“clearFields”或其他名称。 (;

此外,如果您只想重置表单字段,也可以执行以下操作:onClick="this.form.reset()"

关于javascript 函数可能由于范围问题而未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3910937/

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