1

テーブルを実行するスクリプトがありますが、理想的ではありませんが、それを機能させる必要があります。

テーブル フィールドcubicmeterscubesperbundleは、以前のスクリプトによって入力されます。

私がする必要があるのは、テーブルをループして、各行の入力に対して数学関数を実行することです。

行数を持つ変数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));

}

注意すべき点がいくつかあります。このクエリは head ではなく、ページの下部にある script タグ内にあります。その理由は、テーブルに PHP が入力されているため、入力が読み込まれる前に head スクリプトが実行されるためです。そのため、このスクリプトをページの下部に配置しました。さらに、入力され、PHP が入力を入力した後にのみ実行される jquery は、入力され、入力cubicmetersされるように応答します。cubesperbundlecubicmeterscubesperbundle

順序: PHP はproductcode. jquery (ページの下部から呼び出されます) は、に基づいてcubicmetersおよびフィールドにデータを入力します。これはすべて、ユーザー トリガーを必要とせずに、ページの読み込み時に発生する必要があります。cubesperbundleproductcode

bundles最後に、フィールドにcubesperbundle*の結果を入力するには、jquery スクリプトが必要です。cubicmeters

このスクリプトを実行する前の私の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>

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));
});

ありがとう

4

2 に答える 2

1

you can use the .each() function from jQuery to loop on each line, then you can use $(this) on each loop to work on the current line.

Fiddle example.

EDIT : Updated Fiddle with separated functions

于 2013-04-25T07:03:12.227 に答える
1

ループが取得されない.each()ため、すべて間違った使い方をしていますfor$(this)

ワーキングデモ

編集

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));
});
于 2013-04-25T06:46:42.570 に答える