簡體   English   中英

jQuery,從php獲取結果 <li> 在這個例子中

[英]Jquery, get results from php to <li> in this example

因此,這是一個完整的投票系統,但是現在我需要從php獲取結果並將其輸入到current-rating類中,以代替下面的{$ SCORE}。 需要幫助,一如既往,我非常感激。

HTML循環播放(精巧):

<ul class="star-rating" id="{$ID}">
<li class="current-rating" style="width:{$SCORE}%;"></li>
<li><a href="#" class="one-star"></a></li>
<li><a href="#" class="two-stars"></a></li>
<li><a href="#" class="three-stars"></a></li>
<li><a href="#" class="four-stars"></a></li>
<li><a href="#" class="five-stars"></a></li>
</ul>

<div style="display:none;" id="msgOK-{$ID}">Vote</div>
<div style="display:none;" id="msgWRONG-{$ID}">Wrong</div>
<div style="display:none;" id="msgLOGIN-{$ID}">Login</div>

jQuery:

   $(function() {

    $('ul.star-rating').on('click', 'a', function() { // jquery 1.7+

      var score = this.className,
      id = $(this).closest('ul')[0].id;

      $.ajax({ 
            type: "POST",
            url: "js/ajax.php",
            data: {id: id, score: score},
            success: function(pass) {
                if(pass == '1') {
                          // all ok
                     $("div#msgOK-" + id).delay(200).fadeIn(2000).delay(3000).fadeOut(2000);
                     $("ul.star-rating#" + id).hide(100).delay(7100).show('fast');
                } else if(pass == '2') { 
                          // something went wrong
                     $("div#msgWRONG-" + id).delay(200).fadeIn(2000).delay(3000).fadeOut(2000);
                     $("ul.star-rating").hide(100).delay(7100).show('slow');
                } else if(pass == '3') {
                             // you need to be logged in to vote
                     $("div#msgLOGIN-" + id).delay(200).fadeIn(2000).delay(3000).fadeOut(2000);
                     $("ul.star-rating").hide(100).delay(7100).show('slow');
                }
             }
          });
         return false;
       });
    });

為什么不確定CSS寬度內是否有一個用戶分數,我不確定,但是這是您如何將新評級插入CSS寬度屬性中。 在您的成功函數中添加以下內容:

  success: function(pass) {
            //you need to make 'pass' an object on your php side with fields
            //more on that below
            if(pass.errorlvl == '1') {
              $(".current-rating").css('width', pass.userscore );
              $("div#msgOK-" + id).delay(200).fadeIn(2000).delay(3000).fadeOut(2000);
              $("ul.star-rating#" + id).hide(100).delay(7100).show('fast');
            }
            ....

您的php應該像這樣使“傳遞”到對象(或關聯數組)中:

 $myerror_lvl = 1
 $new_score = "3.5"
 return array("errorlvl": $myerror_lvl, "userscore": $new_score);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM