2013-05-09 86 views
0

我已經使用了五星評級系統(基於jQuery和PHP)的我的一個客戶提供in this link。現在,他們希望如果用戶對產品進行評價,那麼許多恆星應該保持高亮顯示,但在目前的情況下,一旦用戶移出鼠標,恆星不再高亮顯示。PHP五星評級 - 星星不繼續點擊

我已經嘗試了很多,但與click功能mouseout功能衝突。到目前爲止,我使用這個:

HTML

<div id="r1" class="rate_widget"> 
    <div class="star_1 ratings_stars" id="1"></div> 
    <div class="star_2 ratings_stars" id="2"></div> 
    <div class="star_3 ratings_stars" id="3"></div> 
    <div class="star_4 ratings_stars" id="4"></div> 
    <div class="star_5 ratings_stars" id="5"></div> 
    <div class="total_votes">vote data</div> 
</div> 

JS - 我有這個調整了我自己,但沒有成功的一點。

$(document).ready(function() { 

    $('.ratings_stars').hover(
     // Handles the mouseover 
     function() { 
      $(this).prevAll().andSelf().addClass('ratings_over'); 
      $(this).nextAll().removeClass('ratings_vote'); 
     }, 
     // Handles the mouseout 
     function() { 
      $(this).prevAll().andSelf().removeClass('ratings_over'); 
      // can't use 'this' because it wont contain the updated data 
      set_votes($(this).parent()); 
     } 
    ); 


    // This actually records the vote 
    $('.ratings_stars').bind('click', function() { 
     var star = this; 
     var widget = $(this).parent(); 
     count = $(star).attr('id'); 
     var clicked_data = { 
      clicked_on : $(star).attr('class'), 
      widget_id : $(star).parent().attr('id') 
     }; 
     $.post(
      'ratings.php', 
      clicked_data, 
      function(INFO) { 
       widget.data('fsr', INFO); 
       set_votes(widget); 
//$(this).prevAll().andSelf().addClass('ratings_over'); 
    //  $(this).nextAll().removeClass('ratings_vote'); 
     $('#msg').hide().html("you have rated this product with "+count+" stars.").fadeIn(1500); 
     //alert("you have rated this product with"+count); 
      }, 
      'json' 
     ); 
    }); 



}); 

function set_votes(widget) { 

    var avg = $(widget).data('fsr').whole_avg; 
    var votes = $(widget).data('fsr').number_votes; 
    var exact = $(widget).data('fsr').dec_avg; 

    window.console && console.log('and now in set_votes, it thinks the fsr is ' + $(widget).data('fsr').number_votes); 

    $(widget).find('.star_' + avg).prevAll().andSelf().addClass('ratings_vote'); 
    // $(widget).find('.star_' + avg).nextAll().removeClass('ratings_vote'); 
    $(widget).find('.total_votes').text(votes + ' votes recorded (' + exact + ' rating)'); 
} 

在當前形勢下,一旦明星們點擊它顯示了更新的平均得分從PHP計算腳本返回。

我希望星星在點擊後甚至在mouseout之後保持高亮,如果沒有點擊它們,mouseout應該起作用,即它不起作用。

請幫幫忙,我絕望。

+0

這聽起來像一些相關的'CSS' – MISJHA 2013-05-09 12:51:17

+0

@MISJHA沒有的東西,它的JS只因爲它是處理除了點擊功能的鼠標移開功能了。 – coder101 2013-05-09 12:53:36

+0

只是一個想法,將「數據庫」等級更改爲差異類別不是更容易,該類別是每個星級元素的基礎。然後嚴格地將懸停類用於懸停,最後當用戶做出選擇時,設置一個選擇的類,在其CSS中使用'!important'來覆蓋所有其他可用的類。 – SpYk3HH 2013-05-09 12:55:29

回答

1

在其當前設置中,set_votes(widget)方法不知道知道,無論用戶是否真正投了票。
您可以通過修改你的「click」事件處理程序,並在您的AJAX成功回調添加一個數據很容易補充一點:

... 
function(INFO) { 
    widget.data('fsr', INFO); 
    // Add user's voting to data: 
    widget.data('fsr').own_voting = count; 

    set_votes(widget); 
    $('#msg').hide().html("you have rated this product with "+count+" stars.").fadeIn(1500); 
    //alert("you have rated this product with"+count); 
} 
... 

然後,你必須也修改set_votes(widget)方法使用這些信息:

function set_votes(widget) { 

    var avg = $(widget).data('fsr').whole_avg; 
    var votes = $(widget).data('fsr').number_votes; 
    var exact = $(widget).data('fsr').dec_avg; 

    var own = $(widget).data('fsr').own_voting; 

    // set voting to own if it is defined, else to avg 
    // also set class to distinguish avg from own 
    if(typeof own != 'undefined') { 
     voting = own; 
      class = 'ratings_over'; 
    } else { 
     voting = avg; 
      class = 'ratings_vote'; 
    } 

    window.console && console.log('and now in set_votes, it thinks the fsr is ' + $(widget).data('fsr').number_votes); 

    // replaced avg with voting 
    // remove all classes to make sure nothing bugs 
    $(widget).find('.star_' + voting).nextAll() 
      .removeClass('ratings_vote').removeClass('ratings_over'); 
    $(widget).find('.star_' + voting).prevAll().andSelf() 
      .removeClass('ratings_vote').removeClass('ratings_over').addClass(class); 
    $(widget).find('.total_votes').text(votes + ' votes recorded (' + exact + ' rating)'); 
} 

我不是很熟悉的JS,所以undefined部分可能是錯誤的 - 但據我所知它應該像這樣工作。


如果你想在widget中顯示用戶的投票頁面已經被重新加載即使,你將不得不在own_voting字段添加到您的服務器對抓取AJAX調用響應。

+0

我想我沒有解釋得很好,問題是,在鼠標移出功能後,星星在點擊發射後變得不明顯。你的代碼正在修改set_votes,我認爲這沒有什麼區別。 – coder101 2013-05-09 13:17:18

+0

這很重要,因爲所有'mouseout'事件處理程序都會重置widget的'ratings_over'類,並調用'set_votes(widget)'。然後,修改的'set_votes(widget)'將小部件設置爲'avg'(如果用戶從未投票)或新的'自己'(如果用戶投票)。 – Lukas 2013-05-09 13:21:57

+0

它像一個魅力工作..感謝盧卡斯 – coder101 2013-05-09 13:44:33