duanlei7101 2016-08-25 13:14
浏览 120
已采纳

jQuery只获取最后一行的值

I'm retreiving the "In Stock" data using MySQL

        <table class="table table-striped table-bordered table-hover results table-fixed">
          <thead>
            <tr>
                <th class="text-center">#</th>
                <th>Product Name</th>
                <th>Description</th>
                <th>Price</th>
                <th>In Stock</th>
                <th style="width: 20%">Quantity</th>
            </tr>
            <tr class="warning no-result">
              <td colspan="8"><i class="fa fa-warning"></i> No Product Found</td>
            </tr>
          </thead>

          <tbody>

            <?php 
                $query = "SELECT * FROM products";
                $exec = mysqli_query($connection, $query);

                while ($row = mysqli_fetch_array($exec)) {

                  $product_id = $row['product_id'];
                  $product_name = $row['product_name'];
                  $description = $row['description'];
                  $product_quantity = $row['quantity'];
                  $product_price = $row['sell_price'];
            ?>

            <tr>
                <td class="text-center"><?php echo $product_id; ?>
                  <input type="hidden" name="product_id[]" value="<?php echo $product_id; ?>">
                </td>
                       <td><?php echo $product_name; ?></td>
                       <td><?php echo $description; ?></td>
                       <td><?php echo $product_price; ?></td>
                       <td><input type="number" value="<?php echo $product_quantity; ?>" id="qtyResult" disabled></td>
                      <td><input type="number" name="qtyBuy[]" id="qtyBuy" onkeyup="updateStock()"></td>
            </tr>

            <?php } ?>

        </tbody>
      </table>

I want to update the In Stock field automatically when I type a number on the quantity field.

The "San Mig Light's" stock is 500 but when I type a number on quantity field, the stock changes to the last record on the table which is 45.

The rest of the row doesn't also work except for the first one.

Here is my jQuery script.

<script>

  function updateStock() {
     var inputQty = $('#qtyBuy').val();
     var inStock = "<?php echo $product_quantity; ?>"
    $('#qtyResult').val(inStock - inputQty);
   }

 </script>

Table

  • 写回答

1条回答 默认 最新

  • douwen3973 2016-08-25 13:21
    关注

    Try to pass to your function the this and event keywords like:

    <td><input type="number" name="qtyBuy[]" id="qtyBuy" onkeyup="updateStock(this, event)"></td>
    

    Because the ID must be unique, try to appent a number like: qtyBuy1, qtyBuy2...

    And to select these fields you can: find('input[id^="qtyResult"]') : select input field where the id starts with the string "qtyResult".

    So, in your updateStock you can use the parameters to refers to the current values like:

        function updateStock(obj, event) {
            var inputQty = obj.value;
            var inStock = "<?php echo $product_quantity; ?>";
            $(obj).closest('tr').find('input[id^="qtyResult"]').val(inStock - inputQty);
        }
    

    The snippet:

    function updateStock(obj, event) {
      var inputQty = obj.value;
      var inStock = $(obj).closest('tr').find('input[id^="qtyResult"]').val();
      var av = $(obj).closest('tr').find('input[id^="qtyResult"]').data('savedValue');
      if (av == undefined) {
        $(obj).closest('tr').find('input[id^="qtyResult"]').data('savedValue', inStock);
        av = inStock;
      }
      inStock = av;
      console.log(av + ' / ' + inStock + ' / ' + inputQty);
      $(obj).closest('tr').find('input[id^="qtyResult"]').val(inStock - inputQty);
    }
    <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
    
    <table class="table table-striped table-bordered table-hover results table-fixed">
        <thead>
        <tr>
            <th class="text-center">#</th>
            <th>Product Name</th>
            <th>Description</th>
            <th>Price</th>
            <th>In Stock</th>
            <th style="width: 20%">Quantity</th>
        </tr>
        <tr class="warning no-result">
            <td colspan="8"><i class="fa fa-warning"></i> No Product Found</td>
        </tr>
        </thead>
    
        <tbody>
        <tr>
            <td class="text-center">product_id1
                <input type="hidden" name="product_id[]" value="1">
            </td>
            <td>product name1</td>
            <td>description1</td>
            <td>product_price1</td>
            <td><input type="number" value="1" id="qtyResult1" disabled></td>
            <td><input type="number" name="qtyBuy[]" id="qtyBuy1" onkeyup="updateStock(this, event)"></td>
        </tr>
        <tr>
            <td class="text-center">product_id2
                <input type="hidden" name="product_id[]" value="<?php echo $product_id; ?>">
            </td>
            <td>product_name2</td>
            <td>description2</td>
            <td>product_price2</td>
            <td><input type="number" value="1" id="qtyResult2" disabled></td>
            <td><input type="number" name="qtyBuy[]" id="qtyBuy2" onkeyup="updateStock(this, event)"></td>
        </tr>
        </tbody>
    </table>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值