简体   繁体   中英

Laravel How to use button from controller into blade

I'm using ajax to pass data from controller and the controller pass the response html to the ajax, this html contains a button, I want to use this button inside the blade again in jquery function, this button contains data and wants to use this data to pass it again to another controller function

in controller

  <button class="remove_this OrderItem_action Button_root" data-id="'.$Product->rowId.'" data-price="'.$Product->price*$Product->qty.'" data-qty="'.$Product->qty.'" type="submit">Remove</button>

I want to implement this function using the passed button, like this example but it doesnt work, how can I do this

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

You should pass the data like $Product->rowId to the blade with return view('index')->with($Product) . Then you can call your variable $Product in your blade like {{ $Product->id }}

In your situation:

Controller

return view('index')->with($Product)

Blade

<button class="remove_this OrderItem_action Button_root" data-id="{{ $Product->rowId }}" data-price="{{ $Product->price*$Product->qty }}" data-qty="{{ $Product->qty }}" type="submit">Remove</button>

then you can pass your jquery in your blade using the @push() method

@push('scripts')
  <script>
      $(function(){ 
          $('.remove_this').on("click", function () { 
              alert('just test');
          });
      });
  </script>
@endpush

你可以解决这个问题,只是使用这个

       $(document).on('click','.remove_this', function () { ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM