我有一个应用程序,用户student_group
在其中添加了一个他们声明该组中有多少人的应用程序students
。我正在尝试从这个线程(我通过 js2coffee.org 更改为咖啡脚本)调整 js 以使用我的应用程序,但我之前从未学习或使用过 js,所以我遇到了一些麻烦。谢谢你的帮助!
student_groups.js.coffee
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
#
# https://stackoverflow.com/questions/15130587/jquery-add-or-remove-table-row-based-on-inputs
emptyRow = ->
row_i++
@obj = $("<tr></tr>")
@obj.append "<td><input type=\"text\" size=\"5\" value=\"" + row_i + "\"/></td>"
@obj.append "<td><input type=\"text\" size=\"5\" name=\"mm" + row_i + "\" id=\"id_mm" + row_i + "\"\"/></td>"
@obj.append "<td><input type=\"text\" size=\"5\" name=\"dd" + row_i + "\" id=\"id_dd" + row_i + "\"\"/></td>"
@obj.append "<td><input type=\"text\" size=\"5\" name=\"ma" + row_i + "\" id=\"id_ma" + row_i + "\"\"/></td>"
@obj.append "<td><input type=\"text\" size=\"5\" name=\"sr" + row_i + "\" id=\"id_sr" + row_i + "\" value=\"0\"\"/></td>"
# how many applications we have drawed now ?
refresh = (new_count) ->
if new_count > 0
$("#nos_header").show()
else
$("#nos_header").hide()
old_count = parseInt($("tbody").children().length)
# the difference, we need to add or remove ?
rows_difference = parseInt(new_count) - old_count
# if we have rows to add
if rows_difference > 0
i = 0
while i < rows_difference
$("tbody").append (new emptyRow()).obj
i++
else if rows_difference < 0 # we need to remove rows ..
index_start = old_count + rows_difference + 1
$("tr:gt(" + index_start + ")").remove()
row_i += rows_difference
row_i = 0
$(document).ready ->
$("#nos").change ->
refresh $(this).val()
student_groups/new
<%= form_for(@student_group) do |f| %>
<p>
<%= f.label :name, "Enter a nickname for this group" %>
<%= f.text_field :name, placeholder: "..." %>
</p>
<p>
<%= f.label :number_of_students, "How many students are in this group?" %>
<!-- https://stackoverflow.com/questions/10038993/rails-and-forms-drop-down-with-range-of-numbers-and-unlimited -->
<%= f.select :number_of_students, (0..60), :id => "nos" %>
</p>
<table class="student_input_form">
<tbody>
<tr>
<td><%= f.label :name, "What is the student's name?" %></td>
<td><%= f.label :gender, "What is the student's gender?" %></td>
</tr>
<%= f.fields_for :students do |builder| %>
<%= render 'students/form', :f => builder %>
<% end %>
</tbody>
</table>
<%= f.submit "Submit", :class => 'big_button round unselectable' %>
<% end %>
最后,`students/_form'
<tr id="nos_header" style="display:none">
<td><%= f.text_field :name, placeholder: "..." %></td>
<td><%= f.select :gender, ['Female', 'Male', 'Transgender'] %></td>
</tr>
编辑:@mu,以解决您的评论:(1)已修复格式,尽管输入框和输出彼此几乎没有关系。我已经阅读了关于在 SO 上格式化文本/代码的文档,但是仍然没有点击,很清楚。道歉。(2) 取出那个打开的<p>
标签——认为那一定是更改前的遗留物。感谢您指出。(3)至于什么不起作用-我应该更清楚。我的目标是在我最初链接到的页面上实现与此类似的功能。当用户输入学生人数时,coffeescript 应该启动并显示适当数量的字段,以便输入那么多新学生。然而,目前,这并没有发生。我在浏览器中没有收到任何错误(即其他作品) - 但是当我选择学生人数时,页面上没有任何变化。谢谢你的帮助!