请我尝试在rails 5上添加一个onclick事件到我的按钮,当点击这个时,该功能是将行添加到一个表但不添加行作为例外,我需要帮助。
**addRow function**
function addrow(count){
var table = document.getElementById("result_table");
var row = document.getElementById("result_table").lastChild;
var clone = row.cloneNode(true);
for (i=0; i<count; i++)}{
table.appendChild(clone);
}
}
以上功能位于名为 controlMenus.js 的文件中,该文件位于 C:\ Users \ UserName \ Documents \ department \ app \ assets \ javascripts rails中项目目录。
**The view**
<div = "row">
<div class = "col-8">
<div class = "col-1"></div>
<div class = "col-1 no-padding">
<%= render 'layouts/lecturer_menu' %>
</div>
<div class = "col-5 no-padding margin-left">
<div class = "trans-background no-padding">
<h5 class = "pad-left centered black-font">ENTER RESULT</h5>
<table id = "result_table" border = "1" style="width: 100%;
align: center;">
<th>S/N</th>
<th>MATNO</th>
<th>NAME</th>
<th>COURSE TITLE</th>
<th>COURSE CODE</th>
<th>GRADE</th>
<th>POINT</th>
<tr>
<td>1</td>
<td>HELLO</td>
<td>HELLO</td>
<td>HELLO</td>
<td>HELLO</td>
<td>HELLO</td>
<td>HELLO</td>
</tr>
<% for i in(1..@row.to_i)do %>
<tr>
<td><%= i+1 %></td>
<td>HELLO</td>
<td>HELLO</td>
<td>HELLO</td>
<td>HELLO</td>
<td>HELLO</td>
<td>HELLO</td>
</tr>
<% end %>
</table>
<%= form_for :rows, url: {controller: "lectureractivities",
action: "addrow"} do |f| %>
<%= f.number_field :row, {min: 1, class: "fixed-width"} %>
<%= submit_tag :addrows, onclick: "addrows();" %>
<% end %>
</div>
</div>
<div class = "col-1"></div>
</div>
</div>
答案 0 :(得分:2)
什么不起作用?你做了什么调试?你确认js文件正在加载吗?您是否已将任何内容记录到控制台以确保您的js函数变量符合您的预期?首先,你的html中有很多错误,最明显的就是这行
<%= submit_tag :addrows, onclick: "addrows();" %>
您调用函数“addrows”,但您的函数被定义为“addrow”。尝试将其更改为
<%= f.submit_tag :addrows, onclick: "addrow();" %>