doujian1954 2018-07-23 01:18
浏览 116

Laravel从控制器传递按钮值到视图

I have small problem, I need to use button from controller to work for JavaScript in view. I pass the HTML from controller to view, but it seems like the view doesn't see the button which comes from controller. Edited: I can display the data from controller into view blade with no error, what I want to do is just to use the button of these data which passed to view in other jquery function here how I recive the data from controller in view blade using the jquery function which sent from it the url and the data

 success: function (data) { 

 $('#cart_product').html(data); 
   }

controller

public function edit(Request $request,$id){
  foreach($products as $Product){
    echo '<input id="rowId" class="remove_this btn btn-sm btn-danger" type="submit" name="rowId">';
  }
}

view blade

<script>
$(function(){ 
  $('.remove_this').on("click", function () { 
    alert('test');
  });
});
</script>
  • 写回答

2条回答 默认 最新

  • dtqi87613 2018-07-23 05:12
    关注

    First you have to SEND the data from controller to view. To do this you can write something like this (I've added products from request to foreach loop):

    public function edit(Request $request,$id){
      $output = '';
      foreach($request->products as $Product){
        $output .= '<input id="rowId" class="remove_this btn btn-sm btn-danger" type="submit" name="rowId">';
      }
      return view('YOUR-VIEW-PATH', compact('output'));
    }
    

    In the above code you save the data in a variable named $output and then send it to the view.

    in the view you can access it this way:

    {!! $output !!}
    

    If you want to use it in JavaScript, I think you know how to do it.

    <script>
        var inputs = "{!! $output !!}";
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作