2016-11-22 17 views
0

RailsとJQueryでAjaxを使用しようとしています。 JQuery関数の先頭にあるid#を変数にする方法はありますか?私はチェックボックスのチェックリストを持っているので、ユーザがチェックボックスをオンにすると、JQueryが応答します。ここで が私のコードであるJQuery Idは変数である必要があります

<script> 
$(document).ready(function(){ 
    if($("#item.id").is(':checked')) { 
    // how do I get the line above to work on every checkbox 
     alert('checked!'); 
    } else { 
     alert('not checked!'); 
    } 
}); 
</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| %> 
      <%= f.check_box :to_do, :id =>item.id, :value => item.id%> 
      //The line above gives the value to each checkbox 
      <%= item.to_do%><br /> 
     <% end %> 
    <%end%> 
<%end%> 
+0

としてif($("#item.id").is(':checked')) {
を作るもの、あなたがしたいことは、[イベントハンドラ](https://learn.jquery.com/events/handling-events/)であるように見えます – adeneo

+0

'class'を' $( '。checkboxClass') 'または要素型として' $( 'input [type = "checkbox"]') '' id'の代わりに指定してみてください。 –

答えて

1

if($("#<%= @item.id %>").is(':checked')) {

関連する問題