我正在尝试对来自 (http://eighty-b.tumblr.com/post/1569674815/creating-an-ajaxified-star-rating-system-in-rails) 的基于 ajax 的评分系统教程进行一些修改-3)。
该教程非常棒并且有效,但我现在试图让它在一个页面上出现多次。我确定错误出在我正在使用的 jquery 中。无论我在最终的 .submit() 语句中将哪种选择器放在表单上,都只会提交 dom 中的顶部表单。我试过 $(this).closest('form').submit(); 我已经尝试为每个表单添加唯一的 id,但是在 .change() 之后,$(this) 获取了顶部表单。我有点期望 .change() 的行为更像 .click()
让我知道是否需要更多代码。谢谢!
.js...
// Sets up the stars to match the data when the page is loaded.
$(function () {
var checkedId = $('form.rating_ballot > input:checked').attr('id');
$('form.rating_ballot > label[for=' + checkedId + ']').prevAll().andSelf().addClass('bright');
});
$(document).ready(function() {
// Makes stars glow on hover.
$('form.rating_ballot > label').hover(
function() { // mouseover
$(this).prevAll().andSelf().addClass('glow');
},function() { // mouseout
$(this).siblings().andSelf().removeClass('glow');
});
// Makes stars stay glowing after click.
$('form.rating_ballot > label').click(function() {
$(this).siblings().removeClass("bright");
$(this).prevAll().andSelf().addClass("bright");
});
// Submits the form (saves data) after user makes a change.
$('form.rating_ballot').change(function() {
$('form.rating_ballot').submit();
});
});
形式...
<%= content_for(:rating_scripts) do %>
<%= javascript_include_tag 'rating_ballot' %>
<% end %>
<%= form_for(rating_ballot, :remote => true, :html => { :class => 'rating_ballot', :id => 'rating-form-' + audition.id.to_s }) do |f| %>
<%= f.label("value_1", content_tag(:span, '1'), {:class=>"rating", :id=>"1"}) %>
<%= radio_button_tag("rating[value]", 1, current_user_rating == 1, :class => 'rating_button') %>
<%= f.label("value_2", content_tag(:span, '2'), {:class=>"rating", :id=>"2"}) %>
<%= radio_button_tag("rating[value]", 2, current_user_rating == 2, :class => 'rating_button') %>
<%= f.label("value_3", content_tag(:span, '3'), {:class=>"rating", :id=>"3"}) %>
<%= radio_button_tag("rating[value]", 3, current_user_rating == 3, :class => 'rating_button') %>
<%= f.label("value_4", content_tag(:span, '4'), {:class=>"rating", :id=>"4"}) %>
<%= radio_button_tag("rating[value]", 4, current_user_rating == 4, :class => 'rating_button') %>
<%= f.label("value_5", content_tag(:span, '5'), {:class=>"rating", :id=>"5"}) %>
<%= radio_button_tag("rating[value]", 5, current_user_rating == 5, :class => 'rating_button') %>
<%= hidden_field_tag("audition_id", audition.id) %>
<%= f.submit :Submit, {:id=>"submit"} %>
<% end %>