【问题标题】:how to calculate stock with jquery如何用jquery计算库存
【发布时间】:2016-01-20 08:47:22
【问题描述】:

我的英语说得不好。我有一个大问题,因为我不了解 jquery。我想开发一个这样的库存计算:

<table id="tabla">
  <thead>
    <tr>
        <th>producto</th>
        <th>entry</th>
        <th>exit</th>
        <th>stock</th>

    </tr>
  </thead>
  <tbody>
<tr><td class="opening balance">10</td></tr>
    <tr>
        <td class="info">TP031</td>
        <td class="entry">4</td>
        <td class="exit">0</td>

        <td class="stock">14</td>
    </tr>
    <tr>
        <td class="info">SI0352</td>
        <td id="entry">0</td>
        <td class="exit">3</td>

        <td class="stock">11</td>
    </tr>
    <tr>
        <td class="info">MP4012</td>
        <td class="entry">0</td>
        <td class="exit">3</td>
        <td class="stock">9</td>
    </tr>
  </tbody>
</table>

我想用 jquery 来做这件事。我希望它被理解。来自智利的问候。

【问题讨论】:

  • 我已经在下面为你回答了,请检查一下!

标签: jquery balance


【解决方案1】:

这是你需要的:

// First initialize the default variables, with your opening balance
var stockCount = 0;
var updatedStock = Number($(".opening.balance").html()) == "NaN" ? 0 : Number($(".opening.balance").html());
// For each "product", calculates the stock with the previous stock
$("tr.product").each(function(index) {
  stockCount = 0;
  
  // Number is used to convert string to number, so we can calculate
  var entry = Number($("td.entry", $(this)).html());
  var exit = Number($("td.exit", $(this)).html());

  entry = entry == "NaN" ? 0 : entry;
  exit = exit == "NaN" ? 0 : exit;

  var stock = updatedStock + entry - exit;
  updatedStock = stock;

  stockCount += stock;
  $("td.stock", $(this)).html(stock);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table id="tabla">
  <thead>
    <tr>
        <th>producto</th>
        <th>entry</th>
        <th>exit</th>
        <th>stock</th>

    </tr>
  </thead>
  <tbody>
<tr><td class="opening balance">10</td></tr>
    <tr class="product">
        <td class="info">TP031</td>
        <td class="entry">4</td>
        <td class="exit">0</td>

        <td class="stock"></td>
    </tr>
    <tr class="product">
        <td class="info">SI0352</td>
        <td class="entry">0</td>
        <td class="exit">3</td>

        <td class="stock"></td>
    </tr>
    <tr class="product">
        <td class="info">MP4012</td>
        <td class="entry">0</td>
        <td class="exit">3</td>

        <td class="stock"></td>
    </tr>
  </tbody>
</table>

请注意,您已为第二行设置了一个 ID。

您可以将点击事件与按钮关联起来进行计算:)

希望我能帮上忙。

(http://codepen.io/anon/pen/XXVrNd)

【讨论】:

  • 太好了,对我帮助很大,感谢您的帮助,他们非常友好.. saludos desde chile
【解决方案2】:
    <html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>

<script>

    $(document).ready(function(){
        var iTotalStock = 0;
        $('#tabla tbody tr').find('td.stock').each(function () {


            iTotalStock = iTotalStock + parseInt($(this).text(), 10);

        });
        $('#total').val(iTotalStock)
    });
</script>
</head>
<body>

   <table id="tabla">
  <thead>
    <tr>
        <th>producto</th>
        <th>entry</th>
        <th>exit</th>
        <th>stock</th>

    </tr>
  </thead>
  <tbody>
<tr><td class="opening balance">10</td></tr>
    <tr>
        <td class="info">TP031</td>
        <td class="entry">4</td>
        <td class="exit">0</td>

        <td class="stock">14</td>
    </tr>
    <tr>
        <td class="info">SI0352</td>
        <td id="entry">0</td>
        <td class="exit">3</td>

        <td class="stock">11</td>
    </tr>
    <tr>
        <td class="info">MP4012</td>
        <td class="entry">0</td>
        <td class="exit">3</td>
        <td class="stock">9</td>
    </tr>
  </tbody>
</table>
    <br /><br />
      Total<input type="text" id="total"/>
</body>
</html>

【讨论】:

    【解决方案3】:

    据我了解,您需要获取库存总数。

    这可以通过添加所有股票来实现。我们需要使用 jquery each 。

    以下代码将给出总库存数。

    var stockTotal=0;
    
    $(function(){
        $('.stock').each(function(){
            stockTotal= stockTotal+ parseInt($(this).text());
        });
        alert(stockTotal);
    });
    

    工作Fiddle

    希望这会有所帮助。

    -帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      • 1970-01-01
      • 2010-12-02
      相关资源
      最近更新 更多