我有一个包含这些行的表单:
<td> - </td>
<input id="invoice_invoice_line_items_attributes_2_item_type" name="invoice[invoice_line_items_attributes][2][item_type]" type="hidden" value="other" />
<td><input id="invoice_invoice_line_items_attributes_2_description" name="invoice[invoice_line_items_attributes][2][description]" size="30" type="text" /></td>
<td><input class="short" id="invoice_invoice_line_items_attributes_2_quantity" name="invoice[invoice_line_items_attributes][2][quantity]" size="30" type="text" /></td>
<td><input class="short format-currency" id="invoice_invoice_line_items_attributes_2_rate" name="invoice[invoice_line_items_attributes][2][rate]" size="30" type="text" /></td>
<td><input class="short format-currency" id="invoice_invoice_line_items_attributes_2_amount" name="invoice[invoice_line_items_attributes][2][amount]" size="30" type="text" /></td>
</tr>
<tr>
<input id="invoice_invoice_line_items_attributes_3_invoice_id" name="invoice[invoice_line_items_attributes][3][invoice_id]" type="hidden" />
<td> - </td>
<input id="invoice_invoice_line_items_attributes_3_item_type" name="invoice[invoice_line_items_attributes][3][item_type]" type="hidden" value="other" />
<td><input id="invoice_invoice_line_items_attributes_3_description" name="invoice[invoice_line_items_attributes][3][description]" size="30" type="text" /></td>
<td><input class="short" id="invoice_invoice_line_items_attributes_3_quantity" name="invoice[invoice_line_items_attributes][3][quantity]" size="30" type="text" /></td>
<td><input class="short format-currency" id="invoice_invoice_line_items_attributes_3_rate" name="invoice[invoice_line_items_attributes][3][rate]" size="30" type="text" /></td>
<td><input class="short format-currency" id="invoice_invoice_line_items_attributes_3_amount" name="invoice[invoice_line_items_attributes][3][amount]" size="30" type="text" /></td>
</tr>
<tr>
<input id="invoice_invoice_line_items_attributes_4_invoice_id" name="invoice[invoice_line_items_attributes][4][invoice_id]" type="hidden" />
<td> - </td>
<input id="invoice_invoice_line_items_attributes_4_item_type" name="invoice[invoice_line_items_attributes][4][item_type]" type="hidden" value="other" />
<td><input id="invoice_invoice_line_items_attributes_4_description" name="invoice[invoice_line_items_attributes][4][description]" size="30" type="text" /></td>
<td><input class="short" id="invoice_invoice_line_items_attributes_4_quantity" name="invoice[invoice_line_items_attributes][4][quantity]" size="30" type="text" /></td>
<td><input class="short format-currency" id="invoice_invoice_line_items_attributes_4_rate" name="invoice[invoice_line_items_attributes][4][rate]" size="30" type="text" /></td>
<td><input class="short format-currency" id="invoice_invoice_line_items_attributes_4_amount" name="invoice[invoice_line_items_attributes][4][amount]" size="30" type="text" /></td>
</tr>
我希望能够从数量字段,费率字段中选择值,然后计算将在“_amount”文本字段中显示的数量。应锁定金额字段以防止用户更改值。除了for循环我怎么能实现这个目标?
答案 0 :(得分:2)
将css类添加到其中一个输入字段或表行中,然后使用$.each()
迭代它们。
答案 1 :(得分:0)
我会用PHP来做实际的计算。使用AJAX将所有字段值传递给PHP脚本。我建议一起使用jQuery的post函数和serialize函数。现在为PHP确保首先设置您的语言环境。
setlocale(LC_MONETARY,'en_CA');
现在抓住所有费用并将每个费用乘以其数量。您现在有一个号码,但您希望它以货币格式。使用money_format功能。
echo money_format('%n', $cost);
我使用echo,因为你是通过AJAX返回将它发送回JavaScript。最后,要阻止用户编辑表单字段,请使用只读属性或隐藏类型。
<input type="text" readonly="readonly" />
<input type="hidden" />
希望有所帮助!
答案 2 :(得分:0)
如果你把数字放在最后而不是中间:即
invoice_invoice_line_items_attributes_rate_2
而不是
invoice_invoice_line_items_attributes_2_rate
然后你可以这样做:
$('input[id^=invoice_invoice_line_items_attributes_rate],' +
'input[id^=invoice_invoice_line_items_attributes_quantity]').change(
function() {
var grab_n = new RegExp('/[0-9]+$/');
var i = parseInt(grab_n.exec($(this).attr('id')));
$('#invoice_invoice_line_items_attributes_amount_' + i).val(
$('#invoice_invoice_line_items_attributes_quantity_' + i).val() *
$('#invoice_invoice_line_items_attributes_rate_' + i).val() );
});
并将金额字段设为只读。 (请记住在php端检查它,如果用户没有启用javascript,或者试图作弊。)