weixin_33696106 2015-06-24 22:12 采纳率: 0%
浏览 21

如何改善我的Ajax?

I'm trying to figure out if what I'm doing is the right way. I have a comment form and when it gets clicked I'm appending the comment into a div element through Ajax. When the page is refreshed then of course that would disappear and instead of it I have a foreach loop that runs and echos the comments. Since they both have the same CSS attributes they look the same to the user. The reason I'm doing it this way is because the foreach loop gets updated only after a refresh. Is there a better way? Can I update the page directly from the database without refresh? I basically need that every time a user clicks on the comment button that the foreach loop will run again but I couldn't find how to do it. I feel like I'm covering a gun shot with bandage the way I do it at the moment.

Loop:

@foreach($comment as $comments)
 @if($comments->image_id == $image->id)
  <div id="{{$comments->id}}" class="col-md-5 ajaxrules">
  <div class="deletecomment">
    <i class="fa fa-trash-o"></i>
  </div>
  <div class="col-md-2"> 
    <img src="{{$comments->user_avatar}}" class="img-circle buddy">
  </div>
  <div class="hello col-md-10"> 
    <h4>{!! $image->user_name !!}</h4>
    <p class="left">{!!$comments->body!!} </p>
  </div> 
  </div>
 @endif
@endforeach 

//Where I append the comments through Ajax until the refresh that replaces it with the loop

<div class="man">

</div>

Ajax:

 <script>
  $(document).ready(function(){
    $('.send-form').click(function(e){ 
      e.preventDefault();
       var username = "{{ $username }}";
       var one = $('textarea[id="{{$image->id}}"]').val();
       var value = "{{$image->id}}";

       var begin = '<div class="col-md-5 addavatar">'+'<div class="deletecomment">'+'<i class="fa fa-trash-o">'+'</i>'+'</div>'+'<div class="col-md-2">'+'<img src="{{$profile}}" class="img-circle">'+'</div>'+'<div class="hello col-md-10">'+'<h4>' + username +'</h4>'+'<p>'+one+'</p>'+'</div>'+'</div>';

     if(one.length > 0){
       console.log(username);
       $('textarea[id="{{$image->id}}"]').val('');
    $.ajax({
      url: 'comment',
      type: "post",
        beforeSend: function (xhr) {
        var token = $('meta[name="csrf_token"]').attr('content');
        if (token) {
              return xhr.setRequestHeader('X-CSRF-TOKEN', token);
        }                
    }, 
    data: {'id': value, 'comment': one},
    success:function(data){
        $( ".man" ).append([begin]);
    },error:function(){ 
        console.log("error!!!!");
    } 
   });
  }      
 });  
});

 </script>
  • 写回答

1条回答 默认 最新

  • 三生石@ 2015-06-24 22:37
    关注

    You are killing yourself. Manipulate the DOM via javascript code like you do it's really hard work! You are not suppose to write html inside javascript strings, there must be another way!

    And there is... Welcome to AngularJS! In angular you can write your html and assign a javascript controller to it, perform ajax request and after the ajax complete you can bind the returned data to the html automatically! That means the angular refresh your html and do all the work for you. Even perform loop of let's say, row in a table, etc...

    评论

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系