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>
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度