首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >每行实时计算

每行实时计算
EN

Stack Overflow用户
提问于 2013-05-04 02:41:27
回答 1查看 814关注 0票数 1

我有一个表,里面有几个单元格,其中一个是数量,另一个是单价,现在我在计算每一行的小计时遇到了问题(模糊)。我不知道我是否走在正确的轨道上。

代码语言:javascript
复制
<div class="widget-body" style="padding: 10px 0 0;">
    <table class="table table-primary table-bordered" id="tableaux">
        <thead>
            <tr>
                <th>Repair Type</th>
                <th>Detail Part Name</th>
                <th>Quantity</th>
                <th>Unit Price</th>
                <th>Subtotal</th>
                <th>Remove</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>
                    <select style="width: auto;" id="repairtype_id1" name="repairtype_id1" class="repairtype">
                        <option value="1">Single</option>
                        <option value="2">Parts</option>
                        <option value="3">Enhancement</option>
                    </select>
                </td>
                <td><input type="text" name="part_name1" id="part_name1" class="part_name" /></td>
                <td><input type="text" name="qty1" id="qty1" style="width: 50px;" class="qty" /></td>
                <td><input type="text" name="unit_price1" id="unit_price1" style="width: 100px;" class="price" /></td>
                <td class="subtotal"><input type="text" class="subtot" id="subtot1" style="width: 100px;" /></td>
                <td>&nbsp;&nbsp;&nbsp;&#x2716;</td>
            </tr>       
        </tbody>
    </table>
</div> <?php echo br()?>

代码语言:javascript
复制
    <button type="submit" class="btn btn-icon btn-primary glyphicons circle_ok" id="saveRepair"><i></i>Record Repair</button>
</form>

    <button class="btn btn-icon btn-primary glyphicons magic" id="addAnother"><i></i>Add Another Item</button>

function addTableRow(table)
    {
        var $tr = $(table).find("tbody tr:last").clone();
        $tr.find("input,select").attr("name", function(){
            var parts = this.id.match(/(\D+)(\d+)$/);

            return parts[1] + ++parts[2];
        }).attr("id", function(){
            var parts = this.id.match(/(\D+)(\d+)$/);
            return parts[1] + ++parts[2];
    });
    $(table).find("tbody tr:last").after($tr);
  };

$('#addAnother').on( 'click', function () {
        addTableRow($("table"));

        return false;
    });

$('table').live('input.subtot', 'blur', function(){
            var row = $(this).closest('tr');
            $(this).val(
                    $('.qty', row).val()*
                    $('.price', row).val()
            );
        });

这是当前的小提琴。

http://jsfiddle.net/XC3w4/

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-04 02:48:39

以下是您要查找的内容:

代码语言:javascript
复制
$(document).on("change", ".price", function() {
    var units = this.value;
    var quantity = $(this).parent().prev().children("input").val();
    var subtotal = Number(units) * Number(quantity);

    $(this).parent().next().children("input").val(subtotal);
});

以下是更新后的小提琴:http://jsfiddle.net/XC3w4/1/

注意:您必须更改addNewRow方法,因为它克隆了前一行,包括输入中的值!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16365587

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档