動的な商品リスト、価格、数量を含む注文フォームを作成しようとしています。ここで、数量は、ユーザーが任意の金額を入力できる入力フィールドです。この数量入力に従って、フォームを送信する前に合計価格がフォーム上でリアルタイムに更新されます。
whileループ内でmysqlデータベースのテーブル名「PRODUCTS」から商品一覧と価格を表示しています。while ループ内で使用した行は次のとおりです。
<tr>
<td style="width:200px;"><span class="tdlist"><?php echo $item?></span></td>
<td style="width:120px; text-align:center;"><span class="tdlist"><?php echo $price?></span></td>
<td style="width:150px;"><input type='text' maxlength=9 name='item<?php echo $id?>' value='' id='item<?php echo $id?>' style="width:60px;"/><select name='quantity<?php echo $id?>' id='quantity<?php echo $id?>' ><option value='Kg' >Kg.</option><option value='gms' >gms</option></select></td>
</tr>
これは、次のような単純な html です。
<tr>
<td style="width:200px;"><span class="tdlist">Sugar</span></td>
<td style="width:120px; text-align:center;"><span class="tdlist">57</span></td>
<td style="width:150px;"><input type='text' maxlength=9 name='item1' value='1' id='item1' style="width:60px;"/><select name='quantity1' id='quantity1' ><option value='Kg' >Kg.</option><option value='gms' >gms</option></select></td>
</tr>
<tr>
<td style="width:200px;"><span class="tdlist">Rice</span></td>
<td style="width:120px; text-align:center;"><span class="tdlist">98</span></td>
<td style="width:150px;"><input type='text' maxlength=9 name='item2' value='1' id='item2' style="width:60px;"/><select name='quantity2' id='quantity2' ><option value='Kg' >Kg.</option><option value='gms' >gms</option></select></td>
</tr>
次のように、ユーザーがフォームに入力した数量に基づいて合計値を表示する div があります。
<div><span class="thlist">Approx Total:</span> <span id="total" class="total">0</span></div>
私はこれを試しました:
<script type="text/javascript">
$(document).ready(function(){
var tot = $('#total').html();
$('#item<?php echo $id?>').keyup(function(){
var itemValue = $('#item<?php echo $id?>').val() * <?php echo $price?>;
if($(this).val())
{
var tot = $('#total').html();
var totalVal = itemValue + parseInt(tot);
}
else
{
var totalVal = parseInt(tot) - itemValue;
}
$('#total').text(totalVal);
});
});
しかし、これは正しく機能しませんでした。Jquery を使用してこれを行うには、専門家のアドバイスが必要です。助けてください、事前に感謝します。