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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)