星级评分系统仅更新列表中的第一项
我使用本教程实施了星级评分系统 http://eighty-b.tumblr.com/post/1569674815/creating-an-ajaxified-star-rating-system-in-rails-3
一切都在SHOW PAGE中工作 ,但是在INDEX PAGE中 ,由于某些原因,Javascript只更新了INDEX PAGE中的第一本书。 当我点击其他书评时,它仍然只更新第一本书。
我觉得奇怪的另一件事是,当我没有使用CSS隐藏单选按钮并用星号替换时,代码只能在索引页面中工作。
新的铁路请帮助:)
JAVASCRIPT
rating_ballot.js
### Sets up the stars to match the data when the page is loaded. $(function () { $('form.rating_ballot').each(function() { var $form = $(this), checkedInput = $form.find('input:checked'); checkedInput.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. (But only does it to the first Book) $(document).on('change', '.rating_button', function(){ $(this).parent().submit(); }); });
CSS.SCSS
### The code doesn't work when i hide the radio buttons to display stars form.rating_ballot input.rating_button { display: none; } form.rating_ballot label.rating { cursor: pointer; display: block; height: 20px; width: 20px; float: left; } form.rating_ballot label.rating span { display: none; } form.rating_ballot label.rating { background-image: image-url('star-dim.png'); } form.rating_ballot label.rating.bright { background-image: image-url('star-bright.png'); } form.rating_ballot label.rating.glow { background-image: image-url('star-glow.png'); }
VIEWS
show.html.erb(显示)
<div id="book_"> 'ratings/rating', :locals =>{:book => @book} %>