使用jquery / javascript减去两个值

时间:2014-08-13 10:54:06

标签: javascript php jquery

<?php $j=1; ?> 
                               <table class="table">
                                  <thead>
                                     <tr>
                                         <th>Sl No</th>
                                        <th>Item</th>
                                        <th>Current Quantity</th>
                                        <th>Procuring Quantity</th>
                                        <th>Updated Quantity</th>
                                     </tr>
                                  </thead>
        <?php
        $res=$_POST['supply'];
        $result=explode("&",$res);
    for($i=0;$i<count($result);$i++){ ?>

                                  <tbody>
                                     <tr class="info">
                                         <?php 
                                         $name=$result[$i];
                                              $query=mysql_query("select * from item where item_name='$name'");
                                              $row=mysql_fetch_array($query);

                                         ?>
                                         <td><?php echo $j;?></td>
    <td><input type="text" name="name[]"  id="contact_name" required="required" class="form-control" value="<?php  echo $result[$i]; ?>" readonly></td>
                                        <td><input type="text" name="current[]" required="required" class="form-control" value="<?php echo  $row['item_qua'];?>"></td>
                                        <td><input type="text" name="procuring[]"  id="procuring" required="required" class="form-control"></td>
                                        <td><input type="text" name="updated[]"  id="updated" required="required" class="form-control"></td>
                                     </tr>

    <?php $j++; } ?>
         </tbody>
                               </table>

我想按当前数量减去采购数量。我希望结果更新数量。 (当前数量 - 采购数量=更新数量)。

2 个答案:

答案 0 :(得分:0)

试试这个:从先前的输入中读取当前的qunatity并从当前输入中获取qunatity。做减法并添加到更新的数量。

注意 - 您已将id用于当前,采购和更新数量,在完成循环后,您的html将重复id。请删除该部分或指定逻辑以使其唯一id

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(function(){
 // bind change event to procuring quantity
 $('input[name="procuring[]"]').change(function(){
  var $parentTR = $(this).closest('.info');
  // read value for current quantity.
  var current = $parentTR.find('input[name="current[]"]').val();

  // read value for procuring quantity.
  var procuring = $(this).val();

  // do substraction and set value in 5th column for updated quantity.
  var updated = parseFloat(current) - parseFloat(procuring);
  $parentTR.find('input[name="updated[]"]').val(updated );
 });
});

</script>

<强> Demo

答案 1 :(得分:0)

你可以通过.val()获取输入框的值,并将id赋给current [],我将id作为当前值。使用以下代码执行此操作

 <td><input type="text" name="name[]"  id="contact_name" required="required" class="form-control" value="<?php  echo $result[$i]; ?>" readonly></td>
                                        <td><input type="text" name="current[]" id="current" required="required" class="form-control" value="<?php echo  $row['item_qua'];?>"></td>
                                        <td><input type="text" name="procuring[]" id="procuring" required="required" class="form-control"></td>
                                        <td><input type="text" name="updated[]"  id="updated" required="required" class="form-control"></td>
<script>
$(document).ready(function(){
$("input").keyup(function(){
var current = $("#current").val();
var procuring = $("#procuring").val();
$("#updated").val(current - procuring);
});
});
</script>

我希望这有助于你