mardi 22 novembre 2016

Getting the individual checkbox value in JQuery

I have several checkboxes and have a JQuery function that I want to print out the value I give each checkbox when I click on the checkbox. However, 1 keeps printing out. I assign each checkbox an item.id based on a primary key from my db using Rails. Here is my code

<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 In the image I am trying to print out the number by the checkbox(item.id), but in the console it only prints out 1




Aucun commentaire:

Enregistrer un commentaire