dougou5844 2016-09-09 08:53
浏览 28
已采纳

使用JQuery和PHP的私人消息

I am currently working on a Chat Application and ran into a slight hiccup, I want a message that is addressed to a user to only be visible to the user and the sender.

    <script>
  //PRIVATE MESSAGE
  var username = '<?=$_SESSION['username'];?>'.toUpperCase();
  $('#chatbox').children('.msgln').each(function(){
    var msgln = $(this);
    $$select = msgln.find('.messagelinebox');
    $message = $$select.text();
    if($message.indexOf('@') !== -1){
      var $product = $message.substr($message.indexOf("@") + 1);
      $$product = $product.substring(0,7).toUpperCase();
      var receiver = $$product;
      if(username = receiver){
        $(this).show();
      }else if(receiver = username){
        $(this).show();
      }else if(receiver != username){
        $(this).hide();
      }else{
        $(this).show();
      }
    }
  });
</script>

Any help will be appreciated.

  • 写回答

2条回答 默认 最新

  • dongxi3209 2016-09-09 13:40
    关注

    As pointed out by everyone, you're going about things the wrong way.

    However, there are some serious problems with your if() statement. This code:

    if(username = receiver) {
        /* code */
    }
    

    is exactly equivalent to the following:

    username = receiver;
    if(username) {
        /* code */
    }
    

    If you want to test if two things are equal, use ==, not =. Also, if a==b, then there is no need to test for b==a. Your entire if ... else structure can be reduced to the following:

    if (username == receiver) {
        $(this).show();
    }
    else {
        $(this).hide();
    }
    

    Your code only checks for messages that were sent to the current user. It doesn't check for messages sent by the current user. That's another thing you will have to fix.

    But really, do this all on the server. Not in client-side Javascript.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大