2016-11-22 39 views
0

我有几个复选框,并且有一个JQuery函数,当我单击复选框时,我想打印出每个复选框的值。但是,1不断打印出来。我使用Rails根据我的数据库中的主键为每个复选框分配一个item.id。这里是我的代码在JQuery中获取单独的复选框值

<script> 
$(document).ready(function(){ 
    $('input[type="checkbox"]').on("change", function() { 
     var hall_id = $(this).val(); // here is where I want to get the value that I assigned to the checkbox 
    if (this.checked) { 
     console.log(hall_id); 
    } else { 
     console.log(hall_id); 
    } 
}); 
}); 
</script> 



<%= link_to "Logout", root_path %> 

<h1>Hello <%= @user.first_name%> <%= @user.last_name%></h1> 

<%= form_for @item do |f| %> 
    <%= f.label :to_do %>: 
    <%= f.text_field :to_do %><br /> 
    <%= f.hidden_field :email, :value => @user.email%> 


    <%= f.submit %> 
<% end %> 

<% @items.each do |item|%> 
    <%if item.email == @user.email%> 
     <%= form_for @item do |f| %> 
     <%= item.id%> 
      <%= f.check_box :to_do, :value => item.id%> //This is where I assign the value for the check box 
      <%= item.to_do%><br /> 
     <% end %> 
    <%end%> 
<%end%> 

enter image description here 在我试图通过复选框(item.id)打印出来的数字图像,但在控制台中只打印出1

+4

还显示生成的HTML代码如下所示。 – CBroe

回答