如何用 jquery 计算库存
how to calculate stock with jquery
我英语说得不好。我有一个大问题,因为我不了解 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 这样做。我希望它被理解。来自智利的问候。
<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>
据我了解,您需要获得库存总数。
这可以通过添加所有股票来实现。我们需要使用jquery each
。
以下代码将给出库存总数。
var stockTotal=0;
$(function(){
$('.stock').each(function(){
stockTotal= stockTotal+ parseInt($(this).text());
});
alert(stockTotal);
});
工作Fiddle
希望这对您有所帮助。
-求助:)
这就是您所需要的:
// 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 设置为第二行。
您可以将点击事件与按钮相关联来计算:)
希望我有所帮助。
我英语说得不好。我有一个大问题,因为我不了解 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 这样做。我希望它被理解。来自智利的问候。
<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>
据我了解,您需要获得库存总数。
这可以通过添加所有股票来实现。我们需要使用jquery each
。
以下代码将给出库存总数。
var stockTotal=0;
$(function(){
$('.stock').each(function(){
stockTotal= stockTotal+ parseInt($(this).text());
});
alert(stockTotal);
});
工作Fiddle
希望这对您有所帮助。
-求助:)
这就是您所需要的:
// 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 设置为第二行。
您可以将点击事件与按钮相关联来计算:)
希望我有所帮助。