gpt4 book ai didi

Javascript 简单数学练习

转载 作者:行者123 更新时间:2023-11-28 12:35:36 25 4
gpt4 key购买 nike

我有这个代码。

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<input onclick="result.value = m1*m2; m1.value = (+m1.value -1);" type="button" id="minus1" value="minus" />
<input onclick="result.value = m1*m2; m1.value = (+m1.value +1);" type="button" id="plus1" value="plus" />
<input type="textbox" id="m1" value="1" />
<br />
<input onclick="result.value = m1*m2; m2.value = (+m2.value -1);" type="button" id="minus2" value="minus" />
<input onclick="result.value = m1*m2; m2.value = (+m2.value +1);" type="button" id="plus2" value="plus" />
<input type="textbox" id="m2" value="1" />
<br />
<br />
Result :<input type="textbox" id="result" />
</body>
</html>

为什么每次更改任何 + 或 - 按钮时我都会得到 NaN 值???

最佳答案

您正在乘以m1*m2。您应该使用:m1.value*m2.value

在某些浏览器中,m1 将返回 id 为 m1 本身的元素。不过,您想要相乘的只是值,因此 m1.value

编辑后的 ​​HTML:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<input onclick="result.value = m1.value*m2.value; m1.value = (+m1.value -1);" type="button" id="minus1" value="minus" />
<input onclick="result.value = m1.value*m2.value; m1.value = (+m1.value +1);" type="button" id="plus1" value="plus" />
<input type="textbox" id="m1" value="1" />
<br />
<input onclick="result.value = m1.value*m2.value; m2.value = (+m2.value -1);" type="button" id="minus2" value="minus" />
<input onclick="result.value = m1.value*m2.value; m2.value = (+m2.value +1);" type="button" id="plus2" value="plus" />
<input type="textbox" id="m2" value="1" />
<br />
<br />Result :
<input type="textbox" id="result" />
</body>
</html>

如果我可以建议,您应该在 onclick 事件中使用一个函数。事情会更清晰,更容易维护,并且您可以添加更多检查,例如输入的数字是否有效。请参阅下面的示例。

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function update(inputToChange, changeValue) {
var parsedValue = parseFloat(inputToChange.value);
if (isNaN(parsedValue)) {
alert(inputToChange.value+' is not a a valid number.');
}
else {
result.value = m1.value * m2.value;
inputToChange.value = parsedValue + changeValue;
}
}
</script>
</head>
<body>
<input onclick="update(m1, -1)" type="button" id="minus1" value="minus" />
<input onclick="update(m1, 1)" type="button" id="plus1" value="plus" />
<input type="textbox" id="m1" value="1" />
<br />
<input onclick="update(m2, -1)" type="button" id="minus2" value="minus" />
<input onclick="update(m2, 1)" type="button" id="plus2" value="plus" />
<input type="textbox" id="m2" value="1" />
<br />
<br />Result :
<input type="textbox" id="result" />
</body>
</html>

Check demo here .

关于Javascript 简单数学练习,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17318956/

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