(http://eighty-b.tumblr.com/post/1569674815/creating-an-ajaxified-star-rating-system-in-railsからのrailsのajaxベースの評価システムチュートリアルにいくつかの変更を加えようとしています-3)。
チュートリアルは素晴らしく機能しますが、現在、1ページに複数回表示されるようにしています。エラーは私が使用している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 %>