我按照this guide将此功能实施到我的应用
但它不能正常工作,当我点击按钮进行克隆并添加字段时它什么都不做,但当我自动加载页面时,它会克隆一个而且不会更多。
我已经尝试了usig j2.coffee,尝试将文件更改为.js,每一个简单的事情,但没有一个工作,
= _form.html.erb代码
<%= form_for(@task) do |f| %>
<% if @task.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@task.errors.count, "error") %> prohibited this task from being saved:</h2>
<ul>
<% @task.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<table >
<thead>
<tr>
<th> <%= f.label :projeto_id, "Projeto da Atividade" %><br> </th>
<th> <%= f.label :seq, "Raiz Sequencial" %><br> </th>
<th> <%= f.label :descr, "Descrição da Atividade" %><br> </th>
<th> <%= f.label :seqpai, "Ramo Sequencial" %><br> </th>
<th> <%= f.label :hour, "Horas de Execução" %> </th>
<th> <%= f.label :typo, "Tipo da Atividade" %><br> </th>
</tr>
</thead>
<tbody id="jqueryform">
<tr name="tr">
<td name="projetoid"> <%= f.grouped_collection_select :projeto_id, Cliente.order(:name), :projetos, :name, :id, :name, prompt: "Selecionar" %> </td>
<td name="raiz"> <%= f.number_field :seq %> </td>
<td name="descr"> <%= f.text_field :descr %> </td>
<td name="seqpai"> <%= f.number_field :seqpai %> </td>
<td name="hour"> <%= f.number_field :hour %> </td>
<td name="typo"> <%= f.select("typo", {"Analitica" => "Analitica", "Sintetica" => "Sintetica"}) %> </td>
</tr>
</tbody>
</table>
<br>
<span id="writeroot"></span>
<br>
<input type="button" id="moreFields" value="Adicionar Atividade!" />
<br><br><br>
<%= f.submit 'Enviar', class: 'btn btn-primary btn-lg' %>
<% end %>
我的tasks.js.coffee代码
counter = 0
moreFields = ->
counter++
newFields = document.getElementById('jqueryform').cloneNode(true)
newFields.id = ''
newFields.style.display = 'block'
newField = newFields.childNodes
i = 0
while i < newField.length
theName = newField[i].name
if theName
newField[i].name = theName + counter
i++
insertHere = document.getElementById('writeroot')
insertHere.parentNode.insertBefore newFields, insertHere
return
window.onload = moreFields