我创建了一个表单列表更新。每行都有一组文本框。 根据我的查询结果,每个文本框都有自己的值。每列的末尾都有一个选择框。您可以选择VALID或INVALID。如果用户为每行选择INVALID状态,我的目标很简单。数量和价格以及总价值将设置为0,并且将根据已设置为选择的项目/行减少以下计算。如果为INVALID,则所选行将扣除到小计金额。
问题:
以下是我的一些代码:
Here is my jquery section:
$('[id^=qty],[id^=price]').on('change',function() {
var index = this.id.match(/\d+/)[0];
var qty = parseInt($('#qty'+index).val());
var price = parseFloat($('#price'+index).val());
var total = 0;
$('#total'+index).val((qty * price ? qty * price : 0).toFixed(2));
var total = 0;
$('[id^=total]').each(function(index){
total+=parseFloat($(this).val()?$(this).val():0);
});
$('#sum_of_total').val(total.toFixed(2));
var vatable = total / 1.12;
var vatable_amt = vatable * 0.12;
var totalVat = vatable + vatable_amt;
$('#vatable').val(vatable.toFixed(2));
$("#vatable_amount").val(vatable_amt.toFixed(2));
$("#gtotal").val(totalVat.toFixed(2));
});
$('[id^=status_validate]').on('change',function(){
var index = this.id.match(/\d+/)[0];
var status_set = $("#status_validate"+index).val();
var price_set = $("#price"+index).val();
var qty_set = $("#qty"+index).val();
var total_set = $("#total"+index).val();
var temp_price = price_set;
var temp_qty = qty_Set;
var temp_total = tota_set;
console.log(status_set);
if(status_set == 'VALID'){
$("#price"+index).val(price_set);
$("#qty"+index).val(price_set);
$("#total"+index).val(price_set);
}else{
price_set = 0.00;
qty_set = 0.00;
total_set = 0.00;
$("#price"+index).val(price_set);
$("#qty"+index).val(price_set);
$("#total"+index).val(price_set);
}
});
这是循环部分
$val = 0;
foreach($order_info_list->result_array() as $details){
$val++;
$vatable_input = $details['vatable_input'];
$vatable_amount = $details['vatable_amount'];
$total_amount_due = $details['total_amount_due'];
echo "<tr>";
echo "<td><input type='text' name='itemdesc[]' value = ".$details['item_desc']." /></td>";
echo "<td><input type='text' name='qty[]' value=".$details['item_qty']." id='qty{$val}' /></td>";
echo "<td><input type='text' name='price[]' value=".number_format($details['item_price'],2)." id='price$val' /></td>";
echo "<td><input type='text' name='total[]' class='k-textbox' id='total{$val}' style='font-family: courier; text-align: right; background-color: lightgray; color: red' readonly='readonly' value='".$details['total']."' /></td>";
echo "<td width='30%'>";
$stat = $details['status'];
echo "<select name='status[]' id='status_validate[$val]'>";
if($stat == 'VALID'){
echo "<option value='VALID' selected='selected'>VALID</option>";
echo "<option value='INVALID'>INVALID</option>";
}else{
echo "<option value='VALID'>VALID</option>";
echo "<option value='INVALID' selected='selected'>INVALID</option>";
}
echo "</select>";
echo "</td>";
echo "</tr>";
$idx = $details['poid'];
echo "<input type='hidden' name='idx[]' value='{$idx}' />";
$code_x = $details['order_code'];
}
以下是总计算的显示
<tr>
<td></td>
<td></td>
<td></td>
<td>VATable Amount:</td>
<td><input type="text" class="k-textbox" value="0.00" readonly="readonly" style="color: red; text-align: right; font-family: courier" name="vatable" id="vatable" /></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td>VAT Input:</td>
<td><input type="text" class="k-textbox" value="0.00" readonly="readonly" style="color: red; text-align: right; font-family: courier" name="vatable_amount" id="vatable_amount" /></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td>TOTAL AMOUNT DUE:</td>
<td><input type="text" class="k-textbox" value="0.00" readonly="readonly" style="color: red; text-align: right; font-family: courier" name="subtotal" id="gtotal" /></td>
</tr>
答案 0 :(得分:1)
当“INVALID”保存最后一个数据时
$("#price"+index).attr('data-last',price_set);
$("#qty"+index).attr('data-last',qty_set);
$("#total"+index).attr('data-last',total_set);
当用户更改后恢复
restoreLast($("#price"+index));
restoreLast($("#qty"+index));
restoreLast($("#total"+index));
function restoreLast(e){
e.val(e.data('last'));
//trigger automatic computation
e.change();
}