テーブルを実行するスクリプトがありますが、理想的ではありませんが、それを機能させる必要があります。
テーブル フィールド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));
}
注意すべき点がいくつかあります。このクエリは head ではなく、ページの下部にある script タグ内にあります。その理由は、テーブルに PHP が入力されているため、入力が読み込まれる前に head スクリプトが実行されるためです。そのため、このスクリプトをページの下部に配置しました。さらに、入力され、PHP が入力を入力した後にのみ実行される jquery は、入力され、入力cubicmeters
されるように応答します。cubesperbundle
cubicmeters
cubesperbundle
順序: PHP はproductcode
. jquery (ページの下部から呼び出されます) は、に基づいてcubicmeters
およびフィールドにデータを入力します。これはすべて、ユーザー トリガーを必要とせずに、ページの読み込み時に発生する必要があります。cubesperbundle
productcode
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));
});
ありがとう