doutao6653 2015-03-07 06:56
浏览 71
已采纳

如何在jquery中的rate按钮下面显示消息状态?

I have a rate button on my page, created using PHP and jQuery.

When the button is clicked, a message is shown and hidden after a while "#num voted including you"

HTML:

div class="voting_wrapper" id="1001">
    <div class="voting_btn">
        <div class="up_button">&nbsp;</div>
        <span class="up_votes"></span>
    </div>
</div>

jQuery:

 $(document).ready(function() {
     //####### on page load, retrive votes for each content
     $.each( $('.voting_wrapper'), function(){
         //retrive unique id from this voting_wrapper element
         var unique_id = $(this).attr("id");
         //prepare post content
         post_data = {'unique_id':unique_id, 'vote':'fetch'};
        //send our data to "vote_process.php" using jQuery $.post()
         $.post('vote_process.php', post_data,  function(response) {
                //retrive votes from server, replace each vote count text
                 $('#'+unique_id+' .up_votes').text(response.vote_up +' user has voted'); 
             },'json');
     });

     //####### on button click, get user vote and send it to vote_process.php using jQuery $.post().
     $(".voting_wrapper .voting_btn").click(function (e) {

         //get class name (down_button / up_button) of clicked element
         var clicked_button = $(this).children().attr('class');

         //get unique ID from voted parent element
         var unique_id   = $(this).parent().attr("id"); 

         if(clicked_button==='up_button') { //user liked the content
             //prepare post content
             post_data = {'unique_id':unique_id, 'vote':'up'};

             //send our data to "vote_process.php" using jQuery $.post()
             $.post('vote_process.php', post_data, function(data) {

                 //replace vote up count text with new values
                 $('#'+unique_id+' .up_votes').text(data);
                 //thank user for liking the content
                 dataModified = data+' users has voting including you';
                 $('#message-status').hide().html(dataModified).fadeIn('slow').delay(5000).hide(1);
             }).fail(function(err) { 

             //alert user about the HTTP server error
                 alert(err.statusText); 
             });
         }

     });
     //end 

 });

It works fine, but placement of the message is incorrect. When I click the button, that message status shows up at the top of the page, while I need it to be shown below the rate button.

May I know how to add the code to achieve this? I think I could use append(); but I am struggling where and how to add the script. Can anyone help me?

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • dongshui2254 2015-03-07 07:54
    关注

    The reason why is that, is probably because the #message-status element is initially placed outside of the voting_wrapper element. You could move #message-status inside that wrapper, below the .voting_btn button using .insertAfter()

    $('#message-status').hide().html(dataModified).insertAfter('#'+unique_id+' .voting_btn').fadeIn('slow').delay(5000).hide(1);
    

    So that after AJAX call, your HTML becomes:

    <div class="voting_wrapper" id="1001">
        <div class="voting_btn">
            <div class="up_button">&nbsp;ddd</div>
            <span class="up_votes">X</span>
        </div>
        <div id="message-status">X users has voting including you</div>
    </div>
    

    [EDIT]

    If you need the message below the whole voting_wrapper, use this:

    $('#message-status').hide().html(dataModified).insertAfter('#'+unique_id).fadeIn('slow').delay(5000).hide(1);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值