2011-03-06 91 views
1

下面的脚本计算给定项目的小计和总数。使用jquery在键盘上计算值

<script type="text/javascript"> 
$(function(){ 





    $('input[type=button]').click(function(){ 



}); 


$('input[id^=qty]').keyup(function(){ 
var gtotal=0; 
    var parentDiv = $(this).closest('#korokor'); 
    var curprice=parentDiv.find('input[id^=price]').val(); 
    var curqty= this.value; 
    var curtotal=curprice * curqty; 




    parentDiv.find('input[id^=subtotal]').val(curtotal); 



    $('input[id^=subtotal]').each(function(index) { 
    var currentnum= $(this).val(); 
    var intnum =parseInt(currentnum); 
    gtotal=gtotal + intnum; 



    }); 

    $('input[name=supertotal]').val(gtotal); 
    $('input[name=payment]').val(gtotal); 

}); 
}); 
</script> 
<?php  
    $viewcarter=mysql_query("SELECT * FROM prod_table WHERE CURRP=1"); 
?> 



</head> 
<body> 
<table border="1"> 
    <tr> 
    <th>ID</th> 
    <th>PRODUCT</th> 
    <th>QA</th> 
    <th>PRICE</th> 
    <th>QTY BUY</th> 
    <th>SUBTOTAL</th> 
    <th>REMOVE</th> 
    </tr> 
</table> 
<?php 
if(mysql_num_rows($viewcarter)==0){ 
    //no products 

}else{ 
while($row=mysql_fetch_assoc($viewcarter)){ 

?> 

<form name="blabber" method="get" action="expander.php"> 
<div id="korokor"> 
<table border="1"> 


<tr> 
    <td><?php echo $row['PID']; ?></td> 
       <td><?php echo $row['PRODUCT']; ?></td> 
       <td><?php echo $row['QTYHAND']; ?></td> 
       <td><?php echo $row['S_PRICE']; ?></td> 

       <input type="hidden" id="pids" name="ids[]" value="<?php echo $row['PID']; ?>"> 
       <input type="hidden" id="pname" name="product[]" value="<?php echo $row['PRODUCT']; ?>"> 
       <input type="hidden" name="dprice[]" value="<?php echo $row['B_PRICE']; ?>"> 
       <input type="hidden" name="sprice[]" id="<?php echo 'price'.$row['PID']; ?>" value="<?php echo $row['S_PRICE']; ?>"/> 
       <input type="hidden" name="qoh[]" value="<?php echo $row['QTYHAND']; ?>"/> 

       <td><input type="text" name="qbuys[]" id="<?php echo 'qty'.$row['PID']; ?>"/></td> 
       <td><input type="text" name="subtotal[]" id="<?php echo 'subtotal'.$row['PID']; ?>"/></td> 
       <td><a href="pagmasdan.php?action=2&id=<?php echo $row['PID']; ?>"><img src="delete-icon.png"></img></a></td> 
</tr> 




</table> 
</div> 

<?php 
} 
?> 

Grand Total:<input type="text" name="supertotal" value=""/><br/> 
Customer:<input type="text" name="customer" value=""/><br/> 
Payment:<input type="text" name="payment" value=""/><br/> 
<input type="submit" value="sell"/> 
</form> 
<?php 
} 

>

它的工作原理,但我有一个小问题,它的外观: enter image description here

我试图把他们都在同一个表,使其更加赏心悦目,但脚本在keyup中计算值不再有效。我需要一些帮助来修改界面的外观。

回答

1

使用一个表...

<form name="blabber" method="get" action="expander.php"> 
<div id="korokor"> 
<table border="1"> 
    <tr> 
    <th>ID</th> 
    <th>PRODUCT</th> 
    <th>QA</th> 
    <th>PRICE</th> 
    <th>QTY BUY</th> 
    <th>SUBTOTAL</th> 
    <th>REMOVE</th> 
    </tr> 

<?php 
if(mysql_num_rows($viewcarter)==0){ 
    //no products 
}else{ 
while($row=mysql_fetch_assoc($viewcarter)){ 
?> 
//now the loop adds a new row rather than new table for each item in the cart. 
<tr class="cartItem"> 
    <td><?php echo $row['PID']; ?></td> 
    <td><?php echo $row['PRODUCT']; ?></td> 
    <td class="qnty"><?php echo $row['QTYHAND']; ?></td> 
    <td class="unitPrice"><?php echo $row['S_PRICE']; ?></td> 
    <td><input type="text" name="qbuys[]" id="<?php echo 'qty'.$row['PID']; ?>"/></td> 
    <td><input type="text" name="subtotal[]" id="<?php echo 'subtotal'.$row['PID']; ?>"/></td> 
    <td><a href="pagmasdan.php?action=2&id=<?php echo $row['PID']; ?>"><img src="delete-icon.png"></img></a> 
     <input type="hidden" id="pids" name="ids[]" value="<?php echo $row['PID']; ?>"> 
     <input type="hidden" id="pname" name="product[]" value="<?php echo $row['PRODUCT']; ?>"> 
     <input type="hidden" name="dprice[]" value="<?php echo $row['B_PRICE']; ?>"> 
     <input type="hidden" name="sprice[]" id="<?php echo 'price'.$row['PID']; ?>" value="<?php echo $row['S_PRICE']; ?>"/> 
     <input type="hidden" name="qoh[]" value="<?php echo $row['QTYHAND']; ?>"/> 
    </td> 
</tr> 

<?php 
} 
?> 
</table> 
</div> 

这应该可以解决大部分的布局,其表的负载,因为你试图做的就是不是一个好主意。如果JavaScript不能与单个表一起使用,那么它应该进行调整以便它可以。

我会看看现在......

这只是非常粗略的我怕(隐形眼镜干出突然)..但

var runningSub = 0; 
var total = 0; 
$('.cartItem').each(function(){ 
    var qnty = $('this .qnty').html(); 
    var unitPrice = $('this .unitPrice').html(); 
    runningSub + (qnty * unitPrice); 
)}; 
// the cart items have been processed so.. 
if(runningSub >0){ 
    total = (calc for the total price) 
} 
// Now you can set your values.. 

如果运行上面加上需要计算出最后的总和,并将值放到你的页面上,你可能会有东西的作品..或至少是一个方向去进去的好主意..

请看在表格的HTML中,我已经修改它以使javascript更简单来写。

+0

谢谢,但是当你尝试运行它。计算值的脚本将不再起作用。 – user225269 2011-03-06 02:19:57

+0

请看更新 – 2011-03-06 02:35:14