この関数を使用して、列内のすべての値の合計を計算します。結果ボックスを現在のセルと同様に入力ボックスに表示させたいと思います。私は値を参照する必要があるので、2つを混合するための構文にちょっとだけ取り組んでいます。関数の結果を2種類の要素で表示する
jQueryの
$(document).ready(function(){
$('#front_finish, #back_finish').change(function(){
function tally (selector) {
$(selector).each(function() {
var total = 0,
column = $(this).siblings(selector).andSelf().index(this);
$(this).parents().prevUntil(':has(' + selector + ')').each(function() {
total += parseFloat($('td.sum:eq(' + column + ')', this).html()) || 0;
})
$(this).html(total);
});
}
tally('td.total');
});
});
入力
<input type="text" name="glass_total" id="glass_total" value="0" />
表
<table id="data">
<tr>
<th>Finish</th>
<th>Pattern</th>
<th>Price</th>
</tr>
<tr>
<td>Front Finish:</td>
<td><span id="front_finish_name">Mirror</span></td>
<td class="sum" id="front_finish_price">15</td>
</tr>
<tr>
<td>Front Pattern:</td>
<td><span id="front_pattern_name"></span></td>
<td class="sum" id="front_pattern_price"></td>
</tr>
<tr>
<td>Back Finish:</td>
<td><span id="back_finish_name">Mirror</span></td>
<td class="sum" id="back_finish_price">15</td>
</tr>
<tr>
<td>Back Pattern:</td>
<td><span id="back_pattern_name"></span></td>
<td class="sum" id="back_pattern_price"></td>
</tr>
<tr>
<th colspan="2" align="right">Total</th>
<td class="total"></td>
</tr>
もう少しhtmlを表示できますか?私は、テキストボックスと列の関係についてはわかりません。 –
はい。私は投稿を更新しました。実際には両者の間には関係がありません。 PHPを使用して合計の結果を電子メールで送信しようとしています。セル内のテキストではなく、入力値を送信するほうがずっと簡単です。 – Thomas
したがって、glass_totalは列とどのように対話しますか? –