英文:
display the total input rounded up
问题
谢谢你提前帮助。
我在想,怎么才能在数量为奇数的情况下显示总数向上取整。
感谢
以下是代码:
$(document).ready(function() {
$('.variable-field').on("input", function() {
var $row = $(this).closest('tr');
var qty = $row.find('.quantity').val() || 0;
$row.find('.totalcostprice').val(Math.ceil(qty / 3));
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table table-striped table-bordered proposal-table" id="proposal-table">
<thead>
<tr>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="number" class="form-control variable-field quantity" name="Quantity" /></td>
<td><input type="number" class="form-control width-80 totalcostprice" name="TotalCostPrice" readonly /></td>
</tr>
</tbody>
</table>
英文:
thank you in advance for your help.
I was wondering how can I do to display the total rounded up in case of odd number.
Thanks
Here is the code:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function() {
$('.variable-field').on("input", function() {
var $row = $(this).closest('tr');
var qty = $row.find('.quantity').val() || 0;
$row.find('.totalcostprice').val(qty / 3);
});
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table table-striped table-bordered proposal-table" id="proposal-table">
<thead>
<tr>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="number" class="form-control variable-field quantity" name="Quantity" /></td>
<td><input type="number" class="form-control width-80 totalcostprice" name="TotalCostPrice" readonly /></td>
</tr>
</tbody>
</table>
<!-- end snippet -->
答案1
得分: 1
你可以使用 Math.ceil()
。试试这个
$(document).ready(function() {
$('.variable-field').on("input", function() {
var $row = $(this).closest('tr');
var qty = $row.find('.quantity').val() || 0;
$row.find('.totalcostprice').val(Math.ceil(qty / 3));
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table table-striped table-bordered proposal-table" id="proposal-table">
<thead>
<tr>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="number" class="form-control variable-field quantity" name="Quantity" /></td>
<td><input type="number" class="form-control width-80 totalcostprice" name="TotalCostPrice" readonly /></td>
</tr>
</tbody>
</table>
英文:
You can use Math.ceil()
. Try this
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function() {
$('.variable-field').on("input", function() {
var $row = $(this).closest('tr');
var qty = $row.find('.quantity').val() || 0;
$row.find('.totalcostprice').val(Math.ceil(qty / 3));
});
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table table-striped table-bordered proposal-table" id="proposal-table">
<thead>
<tr>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="number" class="form-control variable-field quantity" name="Quantity" /></td>
<td><input type="number" class="form-control width-80 totalcostprice" name="TotalCostPrice" readonly /></td>
</tr>
</tbody>
</table>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论