Mantaining数组和一些嵌套的表单字段是否一致?

时间:2016-12-07 15:24:16

标签: javascript jquery ruby-on-rails cocoon-gem

我正在使用cocoon来处理某些表单字段,但我需要使用页面上其他位置的活动表单字段的值,特别是在其他地方使用的数组上;所以我需要用数组填充表单字段,当一个更改时,更改另一个。

我目前正在做的是,每当数组被修改时,我删除所有表单字段,再次添加它们然后我不能通过索引访问它们并且当一个更改时我不确定如何捕获它修改并获取实际索引,考虑到cocoon如何处理插入和删除,索引与显示的内容不一致。

添加代码供参考:

我有这个功能来清除表单,创建新表单,但不能填充field_coords数组,因为我不确定如何实际访问form_fields。

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>

1 个答案:

答案 0 :(得分:0)

您是否尝试使用脏功能代替.click?

我觉得点击会发送一个空白记录,因为它会点击,你的事件不会发送任何内容,然后用户输入值。

此外,您是否有理由不设置控制器来保存您正在清理的信息&amp;重新进入?