在javascript中比较数字大小比较麻烦。。。下面一段是可以实现的代码,经过测试:就是将文本中获得的字符串转化为数字,然后比较,如果不做这一步,则所比较的方法为字符串比较。
<input type="text" name="leftmoney" value="170">
<input type="text" name="money" value="100000000000">
<input type="button" onClick="checkdata(money,leftmoney)">
<script language=javascript>
function checkdata(money,leftmoney)...{
if((Number(leftmoney.value))<(Number(money.value)))
...{
alert("所输入价格大于上一层任务的价格!");
}
else...{
alert("ff");
}
}
</script>
如果在javascript,使用
var leftmoney=document.all.leftmoney.value;
var lefttime=document.all.money.value;
则无法使用上述代码,因为var类型是无定义的类型.
如果上述代码出现问题,则可采用下列代码,重点是采用了parseInt( )
<html>
<head>
<script language="javascript">
function compare()...{
if(parseInt(document.form1.a.value) < parseInt(document.form1.b.value))...{
alert("a <b");
}else...{
alert("a>b");
}
}
</script>
</head>
<form name=form1>
<input name=a type=text/>
<input name=b type=text/>
<input type=button onclick="compare()" value="compare"/>
</form>
</html>
本文介绍了一种在JavaScript中比较数字大小的有效方法。通过将获取到的字符串转换为数字再进行比较,可以避免字符串比较的问题。文章提供了两种实现方式,一种是使用Number()函数,另一种是使用parseInt()函数。
3758

被折叠的 条评论
为什么被折叠?



