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%>
看起來你想要的是一個[事件處理](https://learn.jquery.com/events/handling-events/) – adeneo
嘗試將'class'指定爲'$('。checkboxClass')'或元素類型爲'$('input [type =「checkbox」]')'而不是'id'。 –