duanqian9503 2017-10-18 16:34
浏览 62
已采纳

如果我正在使用laravel,如何在ajax中编写@if

Here I need a help how to write @if role in the javascript if Im using Laravel and the code in laravel as you see but I want to write the same code in the ajax section

Laravel code:

 <td class="text-center">@if ($post->adver_position==0) TOP @endif @if ($post->adver_position==1) RIGHT @endif @if ($post->adver_position==2) LEFT @endif @if ($post->adver_position==3) BOTTOM @endif</td>

Ajax:

      $('.item' + data.id).replaceWith("<tr class='item" + data.id + "'><td  class='col1'>" + data.id + "</td><td><img src='images/" + data.adver_photo + "'style='width: 50%;margin-left: 35px;border-radius: 3px;border: 1px solid #1a2732;' /></td><td>" + data.adver_title + "</td>"
  • 写回答

2条回答 默认 最新

  • doulao2916 2017-10-18 17:08
    关注

    You can not use blade from the client side, but a solution to your problem would be the next one. You can build the string that goes inside the replaceWith() function and make the comparisons from the java script, for example:

      var content = "<tr class='item" + data.id + "'> "
                    +  "<td  class='col1'>" + data.id + "</td> "
                    +  "<td><img src='images/" + data.adver_photo + "'style='width: 50%;margin-left: 35px;border-radius: 3px;border: 1px solid #1a2732;' /> </td>" 
                    +  "<td>" + data.adver_title + "</td>"
                    +  "<td> ";
    
      switch(data.adver_position) {
        case 0:
            content = content + "TOP";
            break;
        case 1:
            content = content + "RIGHT";
            break;
        case 2:
            content = content + "LEFT";
            break;
        case 3:
            content = content + "BOTTOM";
            break;
        }
    
        content = content + "</td> </tr>"
    
    
         $('.item' + data.id).replaceWith(content);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog