我有一個發票與多家項目。當用戶決定改變價格或項目的量,則大部該項目的被立即通過這兩個jQuery函數更新:如何在jQuery完成計算後保留Rails的數字格式?
// Re-calculate item subtotal when quantity gets changed
$('[id$=quantity]').change(function() {
var quantity = $(this).val();
var price = $(this).closest('.item_fields').find('[id$=price]').val();
recalculate_subtotal($(this), price, quantity);
});
function recalculate_subtotal($element, price, quantity) {
$element.closest('.item_fields').find('#Subtotal').val(price * quantity);
}
這工作得很好。但是,似乎jQuery無法保留小計字段的格式。這就是它看起來像在我的代碼:
<%= text_field_tag 'Subtotal', number_to_currency(f.object.subtotal), :disabled => true %>
因此,當一個用戶創建一個新的項目,小計是由Rails的,例如正確格式化像這樣:$200.00
但此刻他改變項目的價格或數量,所有格式消失,只有赤裸裸的整數仍然存在,例如200
。
有沒有一種方法使用jQuery保持Rails的格式?
更重要的是,在我的情況下,格式是從.yml文件派生的,所以如果用戶希望如此,它應該能夠適應長期不同的格式。
有誰能告訴我該怎麼做?
嘿馬丁,謝謝你的幫助!看起來我必須以某種方式將Yaml文件的格式參數傳遞給jQuery。不過,我還沒有找到辦法。 – Tintin81