gpt4 book ai didi

javascript - 除以零检查

转载 作者:行者123 更新时间:2023-11-28 06:06:28 24 4
gpt4 key购买 nike

正在做我的计算器...想检查我的输入 str 是否有“0”以及是否要警告错误。但如何不检查“/”呢?这是我的功能:

<input type="text"  name="answer" id="t" onkeyup="isAllowedSymbol(this);checkLength(this);" placeholder="Enter data" >

<input type="button" value=" &#247; " onclick="calculator.answer.value += '/';div(this);checkLength(this);" />

function div(input)
{
var input = document.getElementById("t");
var lastElement = (input.value.length-1);
//alert(input.value.charAt(lastElement));
if (input.value.charAt(lastElement) == 'null')
{
alert(" / to Zero");
}
}

最佳答案

您可以使用parseInt来计算字符串的值。

if (parseInt($("#myInput").val()) > 0) {
// Do something...
}

您还可以使用 isFinite 检测除以零:

if (isFinite(1/0)) {
// This won't run
} else {
...
}

isFinite 对于 NaN 也将返回 false:

if (isFinite(NaN)) {
// This won't run
} else {
...
}

关于javascript - 除以零检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36822457/

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