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'。 –