我正在使用cocoon來處理某些表單字段,但我需要使用頁面上其他地方的活動表單字段的值,特別是在其他地方使用的數組上;所以我需要用數組填充表單域,當一個改變時,改變另一個。一個數組和一些嵌套的表單域一致嗎?
我現在正在做的是,每當數組被修改時,我刪除所有表單字段,再次添加它們,但然後我不能通過索引訪問它們,當一個被更改時,我不確定如何捕獲更改並得到修改後的實際索引,如果cocoon處理插入和刪除操作,則索引與顯示的內容不一致。參考
添加代碼:
我有這個功能來清除表單,結交新朋友,但我不確定如何實際訪問form_fields不能從field_coords陣列填充。
function redo_forms(){
$('.remove_point').each(function() {
$(this).trigger('click');
});
for(i=0; i < field_coords.length; i++){
$('.add_point').trigger('click');
//add data here
};
}
而且形式本身被定義爲:
<tbody>
<%= f.fields_for :points do |point| %>
<%= render 'admin/shared/point_fields', {:f => point} %>
<% end %>
<tr id='new_point'>
<td colspan="3">
<%= link_to_add_association t(:add_points), f, :points, {'data-association-insertion-method' => 'before', 'data-association-insertion-node' => '#new_point', :partial => 'admin/shared/point_fields', :class => 'add_point'} %>
</td>
</tr>
</tbody>
和:
<tr class='nested-fields'>
<td><%= f.text_field :lat, :class => 'coordinate_lat span8' %></td>
<td><%= f.text_field :lng, :class => 'coordinate_long span8' %></td>
<td><%= link_to_remove_association t(:remove_point), f, :class => 'remove_point' %></td>
</tr>
請問您可以添加您的代碼以供參考。 – anandh