在我的Rails應用程序中,記錄列表顯示在每條記錄前面的複選框以及用於選擇所有記錄的複選框。我想向文本區域添加或刪除parents_contact_no數據。在選擇複選框時,相應記錄的parents_contact_no應該被添加到以逗號分隔的文本區域中,並且在取消選擇相應記錄的複選框時,相應記錄的parents_contact_no應該被移除。 如果選中全選複選框,則parent_contact_no中的所有記錄都應添加到文本區域。其中數字已在選擇/取消選擇複選框顯示添加/刪除textarea中的值在rails中
文本區域被dispayed
<div class="field">
<%= f.label :sent_to %><br>
<div class="result_div">
<%= f.text_field :sent_to %>
</div>
</div>
.html.erb文件
<% if @contact.empty? %>
<h4>No records to display</h4>
<% else %>
<table>
<thead>
<tr>
<th><%= check_box_tag "contact_nos[]", @contact.map(&:parents_contact_no) %></th>
<th>Roll no</th>
<th>Class</th>
<th>Section</th>
<th>Father name</th>
<th>Parents contact no</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @contact.each do |student| %>
<tr>
<td><%= check_box_tag "contact_no[]", student.parents_contact_no %></td>
<td><%= student.roll_no %></td>
<td><%= student.class_name %></td>
<td><%= student.section %></td>
<td><%= student.father_name %></td>
<td><%= student.parents_contact_no %></td>
<td><%= link_to 'Show', student %></td>
<td><%= link_to 'Edit', edit_student_path(student) %></td>
<td><%= link_to 'Destroy', student, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
如何通過jQuery/JavaScript的做到這一點?
我已經添加了一個腳本,這是一個複選框的變化將添加/刪除文本區域中的聯繫人。我面臨的問題是如何獲取當前複選框的值並將其放入文本區域。
<script>
$(document).ready(function() {
$('[id^=contact_no_]').change(function() {
alert("checked");
alert($('input["name=contact_no[]"]:checked').value());
$('#message_message_text').val("Hello");
});
});
</script>