点击Javascript无法检索特定数据

时间:2017-09-26 08:36:02

标签: javascript jquery

我想在用户之间进行聊天。我成功地制作了聊天框。聊天框包含所有用户的列表,如果我点击其中一个用户,则会弹出消息框并显示我和用户之间的消息。

当我点击用户A时,我和A之间的信息将会显示。问题是,当我点击用户B时,用户名将被更改,但我和A之间的消息将被显示(不会被用户B替换)。当我再次点击用户A时,消息将显示两次。

由于

这是ctp文件:

<div class="chat_box">
            <div class="chat_head"> Chat Box</div>
            <div class="chat_body"> 
            <?php
                $imagePath      =   IMAGE_PATH_FOR_TIM_THUMB.'/'.PROFILE_IMAGE_FULL_DIR.'/';
                foreach ($data as $value) 
            {
            ?>
                <div class="users" data-tab="<?php echo $value['User']['id']?>"><?php echo ucfirst($value['User']['first_name']." ".$value['User']['last_name']);?></div>
            <?php
            }
            ?>
            </div>
        </div>
        <div class="msg_box" style="right:290px; display: none;">
            <div class="msg_head">
            </div>
            <div class="close">x</div>
            <div class="msg_wrap">
                <div class="msg_body">

                    <div class="msg_push"></div>
                </div>
                <div class="msg_footer"><textarea class="msg_input" rows="4"></textarea></div>
            </div>
        </div>

这是javascript:

    <script type="text/javascript">
    $(document).ready(function(){
    $('.chat_head').click(function(){
        $('.chat_body').slideToggle('slow');
    });
    $('.msg_head').click(function(){
        $('.msg_wrap').slideToggle('slow');
    });

    $('.close').click(function(){
        $('.msg_box').hide();
    });

    $(document).on('click','.users',function(e){
    e.preventDefault(); 
    var siteurl = '<?php echo SITE_URL; ?>';
    var user_id = ($(this).attr('data-tab'));
    $('.users').removeClass('active');
    $(this).addClass('active');

    $.ajax({
        type: "POST",
        url: '<?php echo SITE_URL; ?>/admin/messages/getname',
        data: {user_id:user_id},
        success: function(data) 
        {
            $('.msg_wrap').show();
            $('.msg_box').show();
            $('.msg_body').show();
            $('.msg_head').html(data['name']);

            for (var i in data) {
                if(i == 'messages'){
                    if (data['name'] == data['name']) {
                        for(var key in i) {
                            if(data[i][key]['Message']['sender_id'] == user_id){
                                $('<div class="msg_a">'+data[i][key]['Message']['message']+'</div>').insertBefore('.msg_push');
                            }else{
                                $('<div class="msg_b">'+data[i][key]['Message']['message']+'</div>').insertBefore('.msg_push');
                            //  $('.msg_b').html(data[i][key]['Message']['message']);
                            }
                        }
                    }
                }
            }

        },
        complete:function(){

        },
        error:function(exception){alert('Exception:'+exception);}   

    });
    });

});
</script>

这是json数据:

{"messages":[
        {"Message":
            {"id":"1689","sender_id":"313","sender_name":"Tom,"receiver_id":"122","message":"yes?"}},
        {"Message":
            {"id":"1690","sender_id":"122","sender_name":"Me","receiver_id":"313","message":"yeah"}}],
        "name":"Student"

}

请帮助

0 个答案:

没有答案