我有一個簡單和基本的jquery函數,用於計算一些產品的數量*單價+稅和小計,然後是總數。我希望根據用戶在列表框中的選擇,實時顯示jquery,不同稅率的稅收進口情況。我發現了2種產品的基本方法,但對於3種或多種,我的方法不起作用:請幫助我? ;)jquery - 攔截,實時計算和顯示列表框的選擇
這是我的代碼:試玩版[點擊這裏] http://jsfiddle.net/skipperit/px9tb/8/
<script type='text/javascript'>
$(function(){
$('#q1,#pu1,#tax1,#tax2,#q2,#pu2').change(function() {
var q1 = parseFloat($('#q1').val(), 10);
var qval1 = 0;
if (q1 > 0) {
qval1 = q1;
}
var q2 = parseFloat($('#q2').val(), 10);
var qval2 = 0;
if (q2 > 0) {
qval2 = q2;
}
var pu1 = parseFloat($('#pu1').val(), 10);
var puval1 = 0;
if (pu1 > 0) {
puval1 = pu1;
}
var pu2 = parseFloat($('#pu2').val(), 10);
var puval2 = 0;
if (pu2 > 0) {
puval2 = pu2;
}
var subtot1 = 0;
subtot1 = parseFloat(qval1 * puval1);
var subtot2 = 0;
subtot2 = parseFloat(qval2 * puval2);
var subtot = 0;
subtot = subtot1 + subtot2;
var tax1 = parseFloat($('#tax1').val(), 10);
var tax2 = parseFloat($('#tax2').val(), 10);
var ivaprice1 = 0;
var ivaprice2 = 0;
if (tax1 > 0) {
ivaprice1 = subtot1 * tax1/100;
}
if (tax2 > 0) {
ivaprice2 = subtot2 * tax2/100;
}
var ivaprice = 0;
ivaprice = parseFloat(ivaprice1 + ivaprice2);
var ivatext = "";
ivatext = "tax ";
$('#total').html(parseFloat(subtot + ivaprice).toFixed(2));
$('#tasse').html(parseFloat(ivaprice).toFixed(2));
$('#imp').html(parseFloat(subtot).toFixed(2));
$('#subtot1b').html(parseFloat(subtot1).toFixed(2));
$('#subtot2b').html(parseFloat(subtot2).toFixed(2));
if (tax2 == tax1) {
$('#taxw').html(parseInt(tax1));
$('#taxtxt2').html('');
$('#taxw2').html('');
$('#taxv').html(parseFloat(ivaprice).toFixed(2));
$('#taxv2').html('');
$('#taxtxt2b').html('');
}
else
{
$('#taxw').html(parseInt(tax1));
$('#taxw2').html(parseInt(tax2));
$('#taxtxt2').html('tax');
$('#taxtxt2b').html('%:');
$('#taxv').html(parseFloat(ivaprice1).toFixed(2));
$('#taxv2').html(parseFloat(ivaprice2).toFixed(2));
}
});
});
</script>
</head>
<body>
<p>q1 :
<input id="q1" value="0"/>
pu1 :
<input id="pu1" value="0"/>
tax1:
<select name="tax1" id="tax1">
<option value="4">4</option>
<option value="21">21</option>
<option value="16">16</option>
</select>
</p>
<p>q2 :
<input id="q2" value="0"/>
pu2 :
<input id="pu2" value="0"/>
tax2 :
<select name="tax2" id="tax2">
<option value="4">4</option>
<option value="21">21</option>
<option value="16">16</option>
</select>
<br>
<br>subtot1: <span id="subtot1b"></span><br>
subtot2: <span id="subtot2b"></span><br>
tax <span id="taxw"></span>%: <span id="taxv"></span><br>
<span id="taxtxt2"></span> <span id="taxw2"></span><span id="taxtxt2b"></span> <span id="taxv2"></span><br>
taxable: €<span id="imp"></span><br>
Tax: €<span id="tasse"></span><br>
Total price : €<span id="total"></span></p>
</body>
TKS的更正;) – user1558177 2012-07-27 18:02:15