$('document').ready(function(){
var totp=11;
var ids="";
for(var c=1;c < totp;c++){
var rateid='input#'+'rate'+c;
var qtyid='input#'+'qty'+c;
var discid='input#'+'disc'+c;
var surcid='input#'+'surc'+c;
var totid='input#'+'tot'+c;
var vatid='input#'+'vat'+c;
ids = ids + rateid+","+qtyid+","+discid+","+surcid+","+vatid+",";
var idsl=ids.replace(/,+$/,'');
$(idsl).keyup(function(){
calcTotal(totp);
});
}
// alert(idsl);
$('#addProd').click(function(){
$('#newProd').fadeIn('fast');
});
});
function calcTotal(rate){
var ftotv=0;
var fvatamt=0;
for(var d=1;d < rate;d++){
var rateid='input#'+'rate'+d;
var qtyid='input#'+'qty'+d;
var discid='input#'+'disc'+d;
var surcid='input#'+'surc'+d;
var totid='input#'+'tot'+d;
var vatid='input#'+'vat'+d;
var ratev=$(rateid).val();
var qtyv =$(qtyid).val();
var discv=$(discid).val();
var surcv=$(surcid).val();
//surcv=parseInt(surcv);
var totv=ratev * qtyv;
// alert(surcv);
var vatamt=totv*5/100;
var totd=totv-discv;
var totv=totd + surcv;
totv=parseInt(totv)+parseInt(vatamt);
$(totid).val(totv);
ftotv=ftotv+totv;
$(vatid).val(vatamt);
fvatamt=fvatamt+vatamt;
}
$(ftotvat).val(fvatamt);
$(ftotid).val(ftotv);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<style>
.newProd {
display: none
}
</style>
<form action='generateinvoice' method='POST'>
<input type='hidden' name='category' value='<br />
<b>Notice</b>: Undefined variable: cat in <b>C:\Program Files\EasyPHP-DevServer-14.1VC11\data\localweb\mechelec\Admin\addsale.php</b> on line <b>5</b><br />
'>
<div style="margin:0 auto">
<div style='margin-left:0;text-align:left'>
<div style='margin-top:30px;text-align:left'>ORDER</div><div class='company' style='margin-top:-20px'><div>
<h3>Company</h3>
<p>
<select name='CompanyId'><option value='3'>Silitech</option></select>
</p></div><div><h3>Purchase Order No</h3><p><input type='text' name='PurchaseOrderNo' style='width:150px' required></div></p><div><h3>Mode Of Payment</h3><p><input type='text' name='ModeOfPayment' required></div></p><p style='clear:both'></p></div> <div>PRODUCT</div><div><input type='hidden' name='InvoiceNo' style='width:150px'></div><br>
</div>
<div class='product'>
<span id='addProd'>Add Item</span><table><tr><th style='width:25%;'>Product Name</th><th style='width:5%'>Quantity</th><th style='width:10%'>Unit</th><th style='width:10%'>Rate</th><th style='width:10%'>Discount</th><th style='width:10%'>Surcharge</th><th style='width:10%'>VAT</th><th style='width:15%'>Total</th></tr><tr>
<td>
<select name='ProductId[]'><option value='14'>Annealing Furnace</option><option value='13'>Gold Melting Furnace</option><option value='12'></option><option value='11'></option></select>
</td><td style='width:10%'><input type='text' name='Quantity[]' id='qty1' required></td><td style='width:10%'>
<select name='Unit[]'><option value='Piece'>Piece</option><option value='Box'>Box</option><option value='Kg'>Kg</option></select>
</td><td><input type='text' name='Rate[]' id='rate1'required></td><td><input type='text' name='Discount[]' id='disc1'></td><td><input type='number' name='Surcharge[]' id='surc1'></td><td><input type='text' name='VAT[]' id='vat1'></td><td><input type='text' name='Total[]' id='tot1'></td></tr><tr>
<td>
<select name='ProductId[]'><option value='14'>Annealing Furnace</option><option value='13'>Gold Melting Furnace</option><option value='12'></option><option value='11'></option></select>
</td><td style='width:10%'><input type='text' name='Quantity[]' id='qty2' required></td><td style='width:10%'>
<select name='Unit[]'><option value='Piece'>Piece</option><option value='Box'>Box</option><option value='Kg'>Kg</option></select>
</td><td><input type='text' name='Rate[]' id='rate2'required></td><td><input type='text' name='Discount[]' id='disc2'></td><td><input type='number' name='Surcharge[]' id='surc2'></td><td><input type='text' name='VAT[]' id='vat2'></td><td><input type='text' name='Total[]' id='tot2'></td></tr><tr>
<td>
<select name='ProductId[]'><option value='14'>Annealing Furnace</option><option value='13'>Gold Melting Furnace</option><option value='12'></option><option value='11'></option></select>
</td><td style='width:10%'><input type='text' name='Quantity[]' id='qty3' required></td><td style='width:10%'>
<select name='Unit[]'><option value='Piece'>Piece</option><option value='Box'>Box</option><option value='Kg'>Kg</option></select>
</td><td><input type='text' name='Rate[]' id='rate3'required></td><td><input type='text' name='Discount[]' id='disc3'></td><td><input type='number' name='Surcharge[]' id='surc3'></td><td><input type='text' name='VAT[]' id='vat3'></td><td><input type='text' name='Total[]' id='tot3'></td></tr><tr>
<td>
<select name='ProductId[]'><option value='14'>Annealing Furnace</option><option value='13'>Gold Melting Furnace</option><option value='12'></option><option value='11'></option></select>
</td><td style='width:10%'><input type='text' name='Quantity[]' id='qty4' required></td><td style='width:10%'>
<select name='Unit[]'><option value='Piece'>Piece</option><option value='Box'>Box</option><option value='Kg'>Kg</option></select>
</td><td><input type='text' name='Rate[]' id='rate4'required></td><td><input type='text' name='Discount[]' id='disc4'></td><td><input type='number' name='Surcharge[]' id='surc4'></td><td><input type='text' name='VAT[]' id='vat4'></td><td><input type='text' name='Total[]' id='tot4'></td></tr><tr>
<td>
<select name='ProductId[]'><option value='14'>Annealing Furnace</option><option value='13'>Gold Melting Furnace</option><option value='12'></option><option value='11'></option></select>
</td><td style='width:10%'><input type='text' name='Quantity[]' id='qty5' required></td><td style='width:10%'>
<select name='Unit[]'><option value='Piece'>Piece</option><option value='Box'>Box</option><option value='Kg'>Kg</option></select>
</td><td><input type='text' name='Rate[]' id='rate5'required></td><td><input type='text' name='Discount[]' id='disc5'></td><td><input type='number' name='Surcharge[]' id='surc5'></td><td><input type='text' name='VAT[]' id='vat5'></td><td><input type='text' name='Total[]' id='tot5'></td></tr><tr>
<td>
<select name='ProductId[]'><option value='14'>Annealing Furnace</option><option value='13'>Gold Melting Furnace</option><option value='12'></option><option value='11'></option></select>
</td><td style='width:10%'><input type='text' name='Quantity[]' id='qty6' required></td><td style='width:10%'>
<select name='Unit[]'><option value='Piece'>Piece</option><option value='Box'>Box</option><option value='Kg'>Kg</option></select>
</td><td><input type='text' name='Rate[]' id='rate6'required></td><td><input type='text' name='Discount[]' id='disc6'></td><td><input type='number' name='Surcharge[]' id='surc6'></td><td><input type='text' name='VAT[]' id='vat6'></td><td><input type='text' name='Total[]' id='tot6'></td></tr><tr>
<td>
<select name='ProductId[]'><option value='14'>Annealing Furnace</option><option value='13'>Gold Melting Furnace</option><option value='12'></option><option value='11'></option></select>
</td><td style='width:10%'><input type='text' name='Quantity[]' id='qty7' required></td><td style='width:10%'>
<select name='Unit[]'><option value='Piece'>Piece</option><option value='Box'>Box</option><option value='Kg'>Kg</option></select>
</td><td><input type='text' name='Rate[]' id='rate7'required></td><td><input type='text' name='Discount[]' id='disc7'></td><td><input type='number' name='Surcharge[]' id='surc7'></td><td><input type='text' name='VAT[]' id='vat7'></td><td><input type='text' name='Total[]' id='tot7'></td></tr><tr>
<td>
<select name='ProductId[]'><option value='14'>Annealing Furnace</option><option value='13'>Gold Melting Furnace</option><option value='12'></option><option value='11'></option></select>
</td><td style='width:10%'><input type='text' name='Quantity[]' id='qty8' required></td><td style='width:10%'>
<select name='Unit[]'><option value='Piece'>Piece</option><option value='Box'>Box</option><option value='Kg'>Kg</option></select>
</td><td><input type='text' name='Rate[]' id='rate8'required></td><td><input type='text' name='Discount[]' id='disc8'></td><td><input type='number' name='Surcharge[]' id='surc8'></td><td><input type='text' name='VAT[]' id='vat8'></td><td><input type='text' name='Total[]' id='tot8'></td></tr><tr>
<td>
<select name='ProductId[]'><option value='14'>Annealing Furnace</option><option value='13'>Gold Melting Furnace</option><option value='12'></option><option value='11'></option></select>
</td><td style='width:10%'><input type='text' name='Quantity[]' id='qty9' required></td><td style='width:10%'>
<select name='Unit[]'><option value='Piece'>Piece</option><option value='Box'>Box</option><option value='Kg'>Kg</option></select>
</td><td><input type='text' name='Rate[]' id='rate9'required></td><td><input type='text' name='Discount[]' id='disc9'></td><td><input type='number' name='Surcharge[]' id='surc9'></td><td><input type='text' name='VAT[]' id='vat9'></td><td><input type='text' name='Total[]' id='tot9'></td></tr><tr>
<td>
<select name='ProductId[]'><option value='14'>Annealing Furnace</option><option value='13'>Gold Melting Furnace</option><option value='12'></option><option value='11'></option></select>
</td><td style='width:10%'><input type='text' name='Quantity[]' id='qty10' required></td><td style='width:10%'>
<select name='Unit[]'><option value='Piece'>Piece</option><option value='Box'>Box</option><option value='Kg'>Kg</option></select>
</td><td><input type='text' name='Rate[]' id='rate10'required></td><td><input type='text' name='Discount[]' id='disc10'></td><td><input type='number' name='Surcharge[]' id='surc10'></td><td><input type='text' name='VAT[]' id='vat10'></td><td><input type='text' name='Total[]' id='tot10'></td></tr><tr><td colspan='6'>Total</td><td><input type='text' name='ftotvat' id='ftotvat'></td><td><input type='text' name='ftotc' id='ftotid'></td></tr></table></div><div style='margin-top:20px;text-align:left'>DELIVERY</div><div class='challan'><div><h3>Delivery Challan No</h3><p><input type='text' name='DeliveryChallanNo' style='width:150px' required></p></div><div><h3>Destination</h3><p><input type='text' name='Destination' style='width:150px' required></p></div><div><h3>Dispatch Through</h3><p><input type='text' name='DispatchThrough' style='width:150px' required></p></div><p style='clear:both'></p></div><div><h3>Remarks</h3><div><textarea style='width:100%;height:100px' name='Remarks' required></textarea></div></div></div>
<input type='submit' name='addsub'></form>
我正在创建一张发票,其中包含费率,数量,增值税(税),折扣和附加费的列,表格中有10行。
当我在每列中输入时,它必须计算行末的最终总数。
一切正常,除了$(surcid),如片段所示,这是附加费输入字段的ID
我已经检查了所有可能的常见问题,但仍然卡在某处。
所有变量按预期计算。除了surcv。
它被连接起来。
这里有什么问题?
答案 0 :(得分:1)
所有变量按预期计算。除了surcv。它被连接起来了。
问题是.val()
会返回一个字符串,如以下代码段所示:
var x=$("#in").val();
console.log(x === "1")
console.log(x === 1)
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='number' id='in' value="1"/>
&#13;
如果对任何值和字符串使用+
,则会得到一个字符串,例如:
var x = 1 + "2"
x==="12"
通过将.val()
转换为数字来解决此问题,有很多方法,包括:
console.log(parseInt($("#num").val(), 10) === 1)
console.log(($("#num").val() * 1) === 1)
console.log(($("#num").val() - 0) === 1)
console.log(~~$("#num").val() === 1)
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="num" type="number" value="1"/>
&#13;
(我个人倾向于使用* 1
,因为太多人错过了,10 on parseInt)
事实上,你的代码已经这样做了,就在它已经连接了字符串之后,你只需要在读取值时将其移动到:
问题的相关代码:
var ratev=$(rateid).val() * 1;
var qtyv =$(qtyid).val() * 1;
var discv=$(discid).val() * 1;
var surcv=$(surcid).val() * 1;
// All the above are now numbers, so can be manipulated fine.
var totv=ratev * qtyv;
var vatamt=totv * 5 / 100;
var totd=totv - discv;
var totv=totd + surcv;
$(totid).val(totv);
您的其他值工作正常,因为它们被操作为数字(*
/ /
/ -
),而+
将按照上面的字符串进行操作。
答案 1 :(得分:0)
我还没有找到问题的根源。但我发现了问题和解决方案。我发现变量$(surcv)输出空白。所以我刚刚添加了
如果(surcv === ''){surcv = 0;}
这解决了我的问题。现在计算是完美的。
无论如何,感谢所有努力帮助我的人。