乘数A*乘数B=积C。
在使用了toFixed(2)后,在b1等于1的情况下,c1的值在验证的时候老是大于a1的值。怎么办?<script language="javascript">
function check(){
if(document.form1.c1.value>document.form1.a1.value){
alert("c1的值不能大于a1!");
return false;
}
function showC(n){
document.form1.c1.value=(n*document.form1.b1.value).toFixed(2);
}
}
</script>
<body>
<form id="form1" name="form1" method="post" action="" onsubmit="return check()">
<table width="100%" height="48" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>乘数A
<input name="a1" type="text" id="a1" value="0" size="6" onkeyup="showC(this.value)" />
乘数B
<input name="b1" type="text" id="b1" value="1" size="6" />
积C
<input name="c1" type="text" id="c1" value="0" size="6" />
<input type="submit" name="Submit" value="提交" /></td>
</tr>
</table>
</form>
在使用了toFixed(2)后,在b1等于1的情况下,c1的值在验证的时候老是大于a1的值。怎么办?<script language="javascript">
function check(){
if(document.form1.c1.value>document.form1.a1.value){
alert("c1的值不能大于a1!");
return false;
}
function showC(n){
document.form1.c1.value=(n*document.form1.b1.value).toFixed(2);
}
}
</script>
<body>
<form id="form1" name="form1" method="post" action="" onsubmit="return check()">
<table width="100%" height="48" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>乘数A
<input name="a1" type="text" id="a1" value="0" size="6" onkeyup="showC(this.value)" />
乘数B
<input name="b1" type="text" id="b1" value="1" size="6" />
积C
<input name="c1" type="text" id="c1" value="0" size="6" />
<input type="submit" name="Submit" value="提交" /></td>
</tr>
</table>
</form>
<script language="javascript">
function check(){
var v1 = document.form1.c1.value;
var v2 = document.form1.a1.value;
v1 = Math.round(v1 * 10000)/10000;
v2 = Math.round(v2 * 10000)/10000;if(v1>v2){
alert("c1的值不能大于a1!");
return false;
}
}
function showC(n){
document.form1.c1.value=(n*document.form1.b1.value).toFixed(2);
}
</script>
<body>
<form id="form1" name="form1" method="post" action="" onsubmit="return check()">
<table width="100%" height="48" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>乘数A
<input name="a1" type="text" id="a1" value="0" size="6" onkeyup="showC(this.value)" />
乘数B
<input name="b1" type="text" id="b1" value="1" size="6" />
积C
<input name="c1" type="text" id="c1" value="0" size="6" />
<input type="submit" name="Submit" value="提交" /></td>
</tr>
</table>
</form>
直接的字符串比较是会出问题的,需要将两个值转化为number才行
document.form1.c1.value - 0 > document.form1.a1.value - 0
这里得到的值是字符串,比较会有问题 ,转换一下成数字比较就可以了因为保留两个小数点,所以乘以100if(parseInt(document.form1.c1.value * 100) > parseInt(document.form1.b1.value * 100)){
//put your code here
}