gpt4 book ai didi

javascript "less than"如果语句失败

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:55:13 24 4
gpt4 key购买 nike

这是我的功能:

function reCalculate(i) {
document.getElementById("Q" + i).value = document.getElementById("C" + i).value - document.getElementById("QA" + i).value;

if (document.getElementById("Q" + i).value < 0) {
document.getElementById("Q" + i).value = 0;
}
if (document.getElementById("Q" + i).value < document.getElementById("E" + i).value && document.getElementById("Q" + i).value != 0) {
alert(document.getElementById("Q" + i).value + " is less than " + document.getElementById("E" + i).value + "?");
document.getElementById("Q" + i).value = document.getElementById("E" + i).value;
}
document.getElementById("Q" + i).value = Math.ceil(document.getElementById("Q" + i).value);
}

它检查 Q,如果小于 0,则将其设为 0。然后,如果它不为 0,但小于 E,则将其设为 E。出于某种原因,除非 Q 是两位数,否则此函数有效。

例如,如果 Q 为 7,E 为 2,那么它会将 Q 保留为 7。但是,如果 Q 为 10,E 为 2,出于某种原因它认为 10<2,并将 Q 更改为 2 !

我是不是漏掉了什么??

最佳答案

当你拉动 .value一个元素,它返回一个字符串。 '10'<'2'将返回 true。

你可以简单地对值做一个 parseInt/parseFloat,ala

var q = parseInt(document.getElementById("Q"+i).value,10)

关于javascript "less than"如果语句失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14056878/

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