gpt4 book ai didi

javascript - javascript 中的最小最大值,没有显示错误,但代码不起作用

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

我真的很陌生,没有出现错误,但代码不起作用。试图获取在文本框中输入的数字的最小值和最大值。

代码如下:

<html lang="en">

<body>
<h1>Problem JavaScript</h1>
Enter a series of numbers with spaces in between each:
<input type="text" name="qty1" id="qty"/>
<button type="button" onclick="calculate();">Enter</button>
</body>
</html

function calculate(){

var numInput = getElementById("qty");
var numArray = numInput.split(" ");

document.write('Min value is:', Math.min.apply(null, numArray));
document.write('Max value is:', Math.max.apply(null, numArray));
}

最佳答案

你几乎明白了。

1.) getElementById 应该是 document.getElementById。参见 document.getElementById() VS. getElementById()

2.) 对 getElementById 返回的元素调用 .split 是行不通的,您需要对元素的值执行此操作。即:

var numInput = document.getElementById("qty").value;
var numArray = numInput.split(" ");

这是一个完整的例子

function calculate() {
var numInput = document.getElementById("qty").value;
var numArray = numInput.split(" ");

document.write('Min value is:', Math.min.apply(null, numArray));
document.write('Max value is:', Math.max.apply(null, numArray));
}
<h1> Problem JavaScript</h1> Enter a series of numbers with spaces in between each:
<input type="text" id="qty">
<input type="button" id="go" value="Go" onclick="calculate()">

关于javascript - javascript 中的最小最大值,没有显示错误,但代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34552740/

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