在 rails 中使用 simple_form 动态添加用户输入

Dynamically add user input with simple_form in rails

我有一个表格,用户可以在其中输入他们每月的开支...

<%= simple_form_for form do |f| %>
<%= f.input :income, label: "Income" %>
<%= f.input :overtime, label: "Overtime" %>
<%= f.input :bonuses, label: "Bonuses" %>
<%= f.input :commission, label: "Commissions" %>
<%= f.input :other_income, label: "Other Income" %>
       *TOTAL HERE*
<% end %>

我希望能够在显示 TOTAL HERE 的字段中添加动态添加用户输入的内容,然后将其保存到数据库中。我玩过字符串插值但没有任何运气。如果有人对我如何做到这一点有任何想法,我将不胜感激。

谢谢

您可以为文本字段编写一个 jQuery keyup 事件,将所有值相加并将其作为值分配到总文本字段中。这会解决你的问题。