使用Ryan Bates优秀的Nested_form gem时,我遇到了一个大问题。事实上,它在我的视图中就像一个魅力,它是一个嵌套的动态添加和删除字段。
问题是,当用户在字段中输入值时,我需要执行一些计算,例如onchange事件。问题是,nested_form gem为所有新添加的行提供了相同的id,然后就不可能选择它来执行某些任务......我必须承认我根本不是Jquery大师...
有人帮我这个。
以下是主视图的代码:
<%= nested_form_for @pinvoice do |f| %>
<% if @pinvoice.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@pinvoice.errors.count, "error") %> prohibited this pinvoice from being saved:</h2>
<ul>
<% @pinvoice.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :contact %>
<%= f.text_field :contact %>
<%= f.label :date_facture %>
<%= f.date_select :date_facture %>
<%= f.label :montant_total %>
<%= f.text_field :montant_total %>
<br />
<br />
</p>
<%= f.fields_for :pinvlines %>
<p><%= f.link_to_add "add one", :pinvlines%>
<p>
<div class="actions">
<%= f.submit %>
</div>
</p>
<% end %>
和“嵌套部分:
的代码<%= javascript_include_tag 'javascript_pinvlines_fields_new' %>
<div class="fields">
<%= f.label :description %>
<%= f.text_field :description %>
<%= f.label :compte_id %>
<%= f.collection_select(:compte_id, @compte, :id, :nom, {:prompt => "Type de charge"}) %>
<%= f.label :quantite %>
<%= f.text_field :quantite, :size => 10 %>
<%= f.label :prix_unitaire %>
<%= f.text_field :prix_unitaire, :size => 10 %>
<%= f.label :montant_HTVA %>
<%= f.text_field :montant_HTVA, :size => 10 %>
<%= f.link_to_remove "remove"%>
</p>
</div>
使用Jquery自动计算名为“montant_HTVA”的字段的代码是“quite_unitire”乘以“quite_unitaire”并使用所有新添加的行....
非常感谢你的帮助。
这是html代码的标准格式,默认情况下有2行,视图+ 2添加了行:
<label for="pinvoice_pinvlines_attributes_0_quantite">Quantite</label>
<input name="pinvoice[pinvlines_attributes][0][quantite]" onchange="calculateMontant();" size="10" type="text" />
<label for="pinvoice_pinvlines_attributes_0_prix_unitaire">Prix unitaire</label>
<input id="pinvoice_pinvlines_attributes_0_prix_unitaire" name="pinvoice[pinvlines_attributes][0][prix_unitaire]" size="10" type="text" />
<label for="pinvoice_pinvlines_attributes_0_montant_HTVA">Montant htva</label>
<input id="montant_HTVA1" name="pinvoice[pinvlines_attributes][0][montant_HTVA]" size="10" type="text" />
<input id="pinvoice_pinvlines_attributes_0__destroy" name="pinvoice[pinvlines_attributes][0][_destroy]" type="hidden" value="false" /><a href="javascript:void(0)" class="remove_nested_fields">remove</a>
</p>
</div></div><div class="fields"><script src="/javascripts/javascript_pinvlines_fields_new.js" type="text/javascript"></script>
<div class="fields">
<label for="pinvoice_pinvlines_attributes_1_description">Description</label>
<input id="pinvoice_pinvlines_attributes_1_description" name="pinvoice[pinvlines_attributes][1][description]" size="30" type="text" />
<label for="pinvoice_pinvlines_attributes_1_compte_id">Compte</label>
<select id="pinvoice_pinvlines_attributes_1_compte_id" name="pinvoice[pinvlines_attributes][1][compte_id]"> <option value="">Type de charge</option>
<option value="1">Caisse</option>
<option value="2">UBS</option>
<option value="3">Ventes</option>
<option value="4">Marketing</option>
<option value="7">Capital</option>
<option value="8">TVA sur ventes</option>
<option value="9">TVA sur achats</option></select>
<label for="pinvoice_pinvlines_attributes_1_quantite">Quantite</label>
<input name="pinvoice[pinvlines_attributes][1][quantite]" onchange="calculateMontant();" size="10" type="text" />
<label for="pinvoice_pinvlines_attributes_1_prix_unitaire">Prix unitaire</label>
<input id="pinvoice_pinvlines_attributes_1_prix_unitaire" name="pinvoice[pinvlines_attributes][1][prix_unitaire]" size="10" type="text" />
<label for="pinvoice_pinvlines_attributes_1_montant_HTVA">Montant htva</label>
<input id="montant_HTVA2" name="pinvoice[pinvlines_attributes][1][montant_HTVA]" size="10" type="text" /> <input id="pinvoice_pinvlines_attributes_1__destroy" name="pinvoice[pinvlines_attributes][1][_destroy]" type="hidden" value="false" /><a href="javascript:void(0)" class="remove_nested_fields">remove</a>
</p>
</div></div>
<p><a href="javascript:void(0)" class="add_nested_fields" data-association="pinvlines">add one</a>
<p>
<div class="actions">
<input id="pinvoice_submit" name="commit" type="submit" value="Create Pinvoice" />
</div>
</p>
</form><div id="pinvlines_fields_blueprint" style="display: none"><div class="fields"><script src="/javascripts/javascript_pinvlines_fields_new.js" type="text/javascript"></script>
<div class="fields">
<label for="pinvoice_pinvlines_attributes_new_pinvlines_description">Description</label>
<input id="pinvoice_pinvlines_attributes_new_pinvlines_description" name="pinvoice[pinvlines_attributes] [new_pinvlines][description]" size="30" type="text" />
<label for="pinvoice_pinvlines_attributes_new_pinvlines_compte_id">Compte</label>
<select id="pinvoice_pinvlines_attributes_new_pinvlines_compte_id" name="pinvoice[pinvlines_attributes] [new_pinvlines][compte_id]"><option value="">Type de charge</option>
<option value="1">Caisse</option>
<option value="2">UBS</option>
<option value="3">Ventes</option>
<option value="4">Marketing</option>
<option value="6">Loyer</option>
<option value="7">Capital</option>
<option value="8">TVA sur ventes</option>
<option value="9">TVA sur achats</option></select>
<label for="pinvoice_pinvlines_attributes_new_pinvlines_quantite">Quantite</label>
<input name="pinvoice[pinvlines_attributes][new_pinvlines][quantite]" onchange="calculateMontant();" size="10" type="text" />
<label for="pinvoice_pinvlines_attributes_new_pinvlines_prix_unitaire">Prix unitaire</label>
<input id="pinvoice_pinvlines_attributes_new_pinvlines_prix_unitaire" name="pinvoice[pinvlines_attributes][new_pinvlines][prix_unitaire]" size="10" type="text" />
<label for="pinvoice_pinvlines_attributes_new_pinvlines_montant_HTVA">Montant htva</label>
<input id="montant_HTVA3" name="pinvoice[pinvlines_attributes][new_pinvlines][montant_HTVA]" size="10" type="text" />
<input id="pinvoice_pinvlines_attributes_new_pinvlines__destroy" name="pinvoice[pinvlines_attributes][new_pinvlines][_destroy]" type="hidden" value="false" /><a href="javascript:void(0)" class="remove_nested_fields">remove</a>
</p>
</div></div></div>
<a href="/pinvoices">Back</a>
</body>
</html>
答案 0 :(得分:0)
我已经改变了执行计算的方式,直接在Rails级别集成它。