代码之家  ›  专栏  ›  技术社区  ›  Smudger

jquery循环遍历行并执行数学函数

  •  1
  • Smudger  · 技术社区  · 13 年前

    我有一个贯穿表格的脚本,这并不理想,但我需要让它发挥作用。

    表格字段 cubicmeters cubesperbundle 由早期脚本填充。

    我现在需要做的是在表中循环,并对每一行的输入执行一个数学函数。

    我有一个变量 rows 其具有行数,并且可以用于将引用每一行的变量I。

    for (var i = 1; i <= rows; i++) {
    
                var cubes = +$(this).closest('tr').find('input[name^="cubicmeters"+i]').val(); // reference the row 'i'
                var cubesperbundle = +$(this).closest('tr').find('input[name^="cubesperbundle"]').val();
                $(this).closest('tr').find('input[name^="bundles"]').val((cubes*cubesperbundle).toFixed(2));
    
    }
    

    需要注意的是,这个查询不在标题中,而是在页面底部的脚本标记中。原因是表中填充了PHP,所以头脚本将在加载输入之前运行。因此,我把这个脚本放在了页面的底部。此外,填充的jquery 立方米 立方体 只有在PHP填充了 立方米 立方体 在上回复以进行填充。

    所以顺序:PHP填充 productcode .jquery(从页面底部调用)填充 立方米 立方体 字段基于 产品编号 。这一切都必须在加载页面时发生,而不需要任何用户触发器。

    最后,我需要jquery脚本来填充 bundles 结果为的字段 立方体 * 立方米

    这个脚本必须运行的html beofre是:

    <table class="authors-list" border=0 id="ordertable">
        <tr>
            <td>Cubic Meters</td><td>Cubes per Bundle</td><td>Total Bundles</td>
        <tr>
           <td><input type="text" name="cubicmeters1" id="cubicmeters1" value="1.38"></td>
           <td><input type="text" name="cubesperbundle1" id="cubesperbundle1" value="1.485"></td>
           <td><input type="text" name="bundles1" id="bundles1"></td>
        </tr>
        <tr>
           <td><input type="text" name="cubicmeters2" id="cubicmeters2" value="1.38"></td>
           <td><input type="text" name="cubesperbundle2" id="cubesperbundle2" value="1.485"></td>
           <td><input type="text" name="bundles2" id="bundles2"></td>
        </tr>
        <tr>
           <td><input type="text" name="cubicmeters3" id="cubicmeters3" value="1.38"></td>
           <td><input type="text" name="cubesperbundle3" id="cubesperbundle3" value="1.485"></td>
           <td><input type="text" name="bundles3" id="bundles3"></td>
       </tr>
    </table>
    

    jsfiddle在这里: http://jsfiddle.net/WfemC/

    这显然不起作用,因此我提出了这个问题。

    谢谢你的帮助。

    更新

    for (var i = 1; i <= rows; i++) {
        (function (index) {
            $.post('get_sku_cubes', {
                data: $('#product' + index).val()
            }, function (result) {
                $('#cubicmeters+ index).val(result);
            });
        })(i)
    }
    
    for (var i = 1; i <= rows; i++) {
        (function (index) {
            $.post('get_cubesperbundle, {
                data: $('#product' + index).val()
            }, function (result) {
                $('#cubesperbundle+ index).val(result);
            });
        })(i)
    }
    
    
    $('#ordertable tr').each(function()
    {
        var cubes = $(this).closest('tr').find('input[name^="cubicmeters"]').val();
    
        var cubesperbundle = $(this).closest('tr').find('input[name^="cubesperbundle"]').val();
        $(this).closest('tr').find('input[name^="totalbundles"]').val((cubes*cubesperbundle).toFixed(2));
    });
    

    谢谢

    2 回复  |  直到 13 年前
        1
  •  1
  •   Dipesh Parmar    13 年前

    你这样做完全是用错了 .each() 因为 for 循环未得到 $(this)

    Working DEMO

    编辑

    HTML格式

    <table class="authors-list" border=0 id="ordertable">
        <tr>
            <td>Cubic Meters</td><td>Cubes per Bundle</td><td>Total Bundles</td>
        <tr>
           <td><input type="text" name="cubicmeters1" id="cubicmeters1" value="1.38"></td>
           <td><input type="text" name="cubesperbundle1" id="cubesperbundle1" value="1.485"></td>
           <td><input type="text" name="bundles1" id="bundles1"></td>
        </tr>
        <tr>
           <td><input type="text" name="cubicmeters2" id="cubicmeters2" value="1.38"></td>
           <td><input type="text" name="cubesperbundle2" id="cubesperbundle2" value="1.485"></td>
           <td><input type="text" name="bundles2" id="bundles2"></td>
        </tr>
        <tr>
           <td><input type="text" name="cubicmeters3" id="cubicmeters3" value="1.38"></td>
           <td><input type="text" name="cubesperbundle3" id="cubesperbundle3" value="1.485"></td>
           <td><input type="text" name="bundles3" id="bundles3"></td>
       </tr>
    </table>
    

    jQuery代码

    $('#ordertable tr').each(function()
    {
        var cubes           = $(this).closest('tr').find('input[name^="cubicmeters"]').val();
        var cubesperbundle  = $(this).closest('tr').find('input[name^="cubesperbundle"]').val();
        $(this).closest('tr').find('input[name^="bundles"]').val((cubes*cubesperbundle).toFixed(2));
    });
    
        2
  •  1
  •   pdegand59    13 年前

    你可以使用 .each() 函数在每一行上循环,然后可以使用 $(this) 在每个环路上工作以在当前线路上工作。

    Fiddle example.

    编辑: Updated Fiddle 具有分离的功能