js中比较数字大小和字符串的匹配对比小结

来源:互联网 发布:dbx pa处理器软件 编辑:程序博客网 时间:2024/06/06 08:19

.toString()方法可将任何数据类型转换成字符类型从而与js中获得的value值相比较

 

 

例如

 

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>测试</title>

<script type=text/javascript >

var str1=document.getElementById("aId1").value

var str2=document.getElementById("aId2").value

 

function check1(){

if(str1=="♧".toString()){           //注意:    .toString() 前面的数据一定要在双引号" "内

alert("yes")

return false

}

 

}

 

//js中数字之间的大小值比较可以采用parseInt(某个value值)转一下再进行对比 如下所示:

function check2(){

if(parseInt(str2.value)>parseInt(str3.value)){

alert("第二行的值大于第三行的值")

return false

}

alert("第二行的值小于第三行的值")

return false

}

 

</script>

<table>

<tr>

<td>

<input type="text" name="" value="♧" id="aId1"/>

</td>

</tr>

<tr>

<td>

<input type="text" name="" value="3" id="aId2"/>

</td>

</tr>

<tr>

<td>

 

<input type="text" name="" value="2" id="aId3"/>

 

</td>

</tr>

</table>

</head>