【问题标题】:Showing the sum of all price text box(`class=price`) in to subTotal textbox(`id=subTotal`)在 subTotal 文本框(`id=subTotal`)中显示所有价格文本框(`class=price`)的总和
【发布时间】:2013-11-23 14:56:13
【问题描述】:

我有以下 html 表结构

<tr class="row" id="item10">
 <td><input type="text" name="barcode" class="barcode" style="width: 50px"/></td>
 <td><input type="text" name="type" class="type" readonly="true" style="width: 50px"/></td>
 <td><input type="text" name="color" class="color" readonly="true" style="width: 50px"/></td>
 <td><input type="text" name="qty" class="qty" style="width: 50px"/></td>
 <td><input type="text" name="unitprice" readonly="true"  class="unitPrice" style="width: 50px"/></td>
 <td><input type="text" name="price" class="price" style="width: 50px"/></td>
 </tr>



<tr  id="SubTotal">
     <td colspan="5">SubTotal</td>
      <td><input type="text" name="subTotal" id="subTotal" style="width: 50px"/></td>
  </tr>
    <tr  id="VAt">
        <td colspan="5">Vat</td>
         <td><input type="text" name="vat" id="vat" style="width: 50px"/></td>
     </tr>
     <tr  id="Total">
          <td colspan="5">Total</td>
            <td><input type="text" name="Total" id="Total" style="width: 50px"/></td>
     </tr>

可能没有&lt;tr class="row"&gt;(动态生成)每个表行由其id标识 itemXXXX 是数字。

我在这里读取产品的条形码,然后将其详细信息填写在适当的文本框中。 然后,当我更改产品数量时,其相应的价格会在文本框中更新为 `class="price"'。

我的问题是当任何产品价格更改或在价格文本框中输入时。 所有产品价格的总和应显示在小计文本框中。

【问题讨论】:

  • 有多个id为SubTotal的元素
  • 没有一个subTotal 存在

标签: jquery


【解决方案1】:

试试这个:

var $inputs = $('input[name=price]');
$inputs.change(function () {
    var sum = 0;
    $inputs.each(function () { 
        sum += +$(this).val() || 0; 
    });
    $('#subTotal').val(sum);
});

【讨论】:

  • 非常有帮助。谢谢。
  • @Arbaaz 谢谢队友^^
【解决方案2】:

试试

$('table').on('change', '.row input[name="price"]', function () {
    var total = 0;
    $('.row input[name="price"]').each(function () {
        total += parseFloat(this.value)
    });
    $('#subTotal').val(total);
})

如果price字段的值被脚本改变了,那么也触发改变事件

.val(x).change()

【讨论】:

  • 当我更改 price 中的任何值时它正在工作,但我希望在价格中填充值(使用 json)时它应该在 `subTotal' 中显示结果
【解决方案3】:

附加 Jquery $table.on('change', 'td.price', function ...) 以检测每个价格字段的变化,然后在回调函数中使用 $.each on 'td.price'总结

【讨论】:

    【解决方案4】:

    您可以使用我提供的以下代码来获得总和,但请确保您必须将以下代码包装在 function 中并在所有情况下都可能调用它(在 blur,change 事件等中,基本上是 @ 987654323@change 事件在价格文本框更改时丢失focus.) 时发生。

    var xSubTotal = 0;
    
    $("tr.row input[name='price']").each(function(){
     xSubTotal += $(this).val();
    });
    
    $("#subTotal").val(xSubTotal);
    

    【讨论】:

    • @Bala 电话、whatsapp 等……都死了???手里拿着智能手机的目的是什么??
    • @Bala Oh.. 对不起,我把手机放在充电器上。我没有注意到你的电话。请原谅我。
    • @Bala 人家都说你有大把的时间来完成绑定,那你干嘛催我快点发简历。。我还以为你自己做的简历比我的还完美呢.你有任何计划像其他智障一样签署债券吗??
    • poda pani ...我需要一些经验,我问你...我会杀了你uuuuuu
    • @Bala 简历就像为即将到来的比赛养马一样。你必须照顾它并根据自己的起伏来制作它。它在市场上应该是非常独特的。我知道你的思维方式比其他人独特,但我想知道你为什么犹豫要自己建立一个新的思维方式?我知道你有能力将想象力发挥到极致。那你为什么不能用它来建立比别人更好的简历???
    猜你喜欢
    • 2019-05-28
    • 1970-01-01
    • 2012-08-29
    • 2022-12-02
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多