[英]How to calculate the grand total of the column only condition that the contents?
抱歉,如果我询问与以前类似的事情,但是在编码javascipt时我已经尝试并感到困惑。 像以前一样,我想以只有isNaN为= 0,如果不是NaN = total的条件来计算总计。 当我尝试条件isNaN时是正确的,但如果不是NaN,他保持总数= 0为什么? 我已经尝试过使用onKeyup,onBlur和onChange,也许我在功能上有问题? 能帮我吗?
<html>
<head>
<form method = "POST" action="text.php" name="form">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="css/jquery-ui.css"/>
<script src="js/jquery-1.9.1.js"></script>
<script src="js/jquery-ui.js"></script>
<!--<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>-->
<script type="text/javascript">
function cal1() {
var a = parseInt($(".qty1").val());
var b = parseInt($(".price1").val());
var c1 = a * b; //a * b
$(".total1").val(c1);
grandtotal();
}
function cal2() {
var a = parseInt($(".qty2").val());
var b = parseInt($(".price2").val());
var c2 = a * b; //a * b
$(".total2").val(c2);
grandtotal();
}
function cal3() {
var a = parseInt($(".qty3").val());
var b = parseInt($(".price3").val());
var c3 = a * b; //a * b
$(".total3").val(c3);
grandtotal();
}
function grandtotal() {
var a1 = parseInt($(".total1").val());
var b1 = parseInt($(".total2").val());
var c1 = parseInt($(".total3").val());
if (isNaN(a1.value) == true)
{
var a1=0;
}
else
{
var a1 = parseInt($(".total1").val());
}
if (isNaN(b1.value) == true)
{
var b1=0;
}
else
{
var b1 = parseInt($(".total2").val());
}
if (isNaN(c1.value) == true)
{
var c1=0;
}
else
{
var c1 = parseInt($(".total3").val());
}
var z = a1+b1+c1; // a1+b1+c1
$(".grandtotal").val(z);
}
</script>
</head>
<body>
<table border="1">
<tr>
<td>
<table height="51" border="0" cellspacing="2">
<tr>
<td>
<div align="center">Price</div>
</td>
<td>
<div align="center"> Total Price</div>
</td>
</tr>
<tr>
<td>
<input class="qty1" type="text" name="qty1" onblur="qty_blur(this);" onkeyup="cal1();" value="">
</td>
<td>
<input class="price1" type="text" name="price1" onblur="price_blur(this);" onkeyup="cal1();" value="">
</td>
<td>
<input class="total1" type="text" name="total1" value="0" readonly=true onkeyup="grandtotal();">
</td>
</tr>
<tr>
<td>
<input class="qty2" type="text" name="qty2" onblur="qty_blur(this);" onkeyup="cal2();" value="">
</td>
<td>
<input class="price2" type="text" name="price2" onblur="price_blur(this);" onkeyup="cal2();" value="">
</td>
<td>
<input class="total2" type="text" name="total2" value="0" readonly=true onkeyup="grandtotal();">
</td>
</tr>
<tr>
<td>
<input class="qty3" type="text" name="qty3" onblur="qty_blur(this);" onkeyup="cal3();" value="">
</td>
<td>
<input class="price3" type="text" name="price3" onblur="price_blur(this);" onkeyup="cal3();" value="">
</td>
<td>
<input class="total3" type="text" name="total3" value="0" readonly=true onkeyup="grandtotal();">
</td>
</tr>
</table>
</td>
</tr>
</table>
<table>
<tr div class="grandtotal">
<td>
Grand Total :
</td>
<td>
<input class="grandtotal" name="grandtotal" type="text" readonly=true value="">
</td>
</tr>
</div>
</table>
</body>
数字没有.value
属性; 只需检查数字本身即可。 另外, isNaN
返回一个布尔值,因此您无需检查== true
。
if (isNaN(a1)) {
var a1=0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.