`
cary1130
  • 浏览: 197491 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JavaScript中数字的比较

    博客分类:
  • java
阅读更多
错误代码:
js 代码

<html>
<head>
<script language="javascript">
function compare(){
 if(document.form1.a.value < document.form1.b.value){
  alert("a must more than b");
 }else{
  alert("ok");
 }

}
</script>
</head>
<form name=form1>
<input name=a type=text/>
<input name=b type=text/>
<input type=button onclick="compare()" value="compare"/>
</form>
</html>

 

正确代码:

js 代码

<html>
<head>
<script language="javascript">
function compare(){
 if(parseInt(document.form1.a.value) < parseInt(document.form1.b.value)){
  alert("a must more than b");
 }else{
  alert("ok");
 }

}
</script>
</head>
<form name=form1>
<input name=a type=text/>
<input name=b type=text/>
<input type=button onclick="compare()" value="compare"/>
</form>
</html>

 

总结:

很显然,document.form1.b.value取得的值是String类型的。so you must chang it from string to int,there i use parseInt() which is a method in JS.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics