显示总输入,四舍五入。

huangapple go评论52阅读模式
英文:

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() {
  $(&#39;.variable-field&#39;).on(&quot;input&quot;, function() {
    var $row = $(this).closest(&#39;tr&#39;);
    var qty = $row.find(&#39;.quantity&#39;).val() || 0;
    $row.find(&#39;.totalcostprice&#39;).val(qty / 3);
  });
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;table class=&quot;table table-striped table-bordered proposal-table&quot; id=&quot;proposal-table&quot;&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Quantity&lt;/th&gt;
      &lt;th&gt;Total&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;input type=&quot;number&quot; class=&quot;form-control variable-field quantity&quot; name=&quot;Quantity&quot; /&gt;&lt;/td&gt;
      &lt;td&gt;&lt;input type=&quot;number&quot; class=&quot;form-control width-80 totalcostprice&quot; name=&quot;TotalCostPrice&quot; readonly /&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

<!-- 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() {
  $(&#39;.variable-field&#39;).on(&quot;input&quot;, function() {
    var $row = $(this).closest(&#39;tr&#39;);
    var qty = $row.find(&#39;.quantity&#39;).val() || 0;
    $row.find(&#39;.totalcostprice&#39;).val(Math.ceil(qty / 3));
  });
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;table class=&quot;table table-striped table-bordered proposal-table&quot; id=&quot;proposal-table&quot;&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Quantity&lt;/th&gt;
      &lt;th&gt;Total&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;input type=&quot;number&quot; class=&quot;form-control variable-field quantity&quot; name=&quot;Quantity&quot; /&gt;&lt;/td&gt;
      &lt;td&gt;&lt;input type=&quot;number&quot; class=&quot;form-control width-80 totalcostprice&quot; name=&quot;TotalCostPrice&quot; readonly /&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月3日 19:29:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626529.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定