我的 html 代码是这样的:
<html>
<SCRIPT type="text/javascript" language="JavaScript">
function fun()
{
var l = document.test.low.value;
var h = document.test.high.value;
alert(l);
alert(h);
if(l >h){
alert("low greater than high low is -"+l+"high is -"+h);
}
}
</SCRIPT>
<body>
<form name="test">
<input type="text" size="11" id="low" />
<input type="text" size="11" id="high" />
<input type="button" value="sss" onClick="fun()"/>
</form>
</body>
</html>
当我比较这两个值时,使用低是 12 和高是 112 是行不通的。 像 22 和 122、33 和 133 等等......
我正在使用 IE 浏览器版本 8。请帮助。
最佳答案
将值转换为数字:
if(Number(l) > Number(h)){ /* … */ }
或者,将每个字符串解析为 float :
if(Number.parseFloat(l) > Number.parseFloat(h)){ /* … */ }
或作为整数:
if(Number.parseInt(l, 10) > Number.parseInt(h, 10)){ /* … */ }
关于javascript - 在javascript中比较两个数字的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9094299/