javascript - 在javascript中比较两个数字的问题

标签 javascript html

我的 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/

相关文章:

javascript - 表索引和成员绑定(bind)

javascript - 在 Codepen 元素中使用 fullpage.js 时遇到问题

css - Bootstrap 3 - CSS 对齐内容

javascript - 将 promise polyfill 添加到 ES6

javascript - 为什么我无法将外部链接设置为 nofollow?

javascript - TinyMCE表: how to hide advanced properties?

html - <h1><a> 不同时间的边界转换

html - css: display:inline-block and span with position:relative 构建多列下拉列表

javascript - HTML 如何处理 Esc 键与其他键

javascript - jExpand.js 可扩展表格