简体   繁体   中英

Why JQuery code does not work when i click on button?

What is the problem with my JQuery code when I click on the button it does not work, I searched a lot and tried many ways but my problem not solved. I used this code in other files, on there it works correctly but it not work in this file.

this my table:

<table class="table table-bordered">
                              
       <tbody>
         <?php
          $insideOrders = \App\OutsideModel::where('total_id', '=', $order->order_id)->get();
                                ?>
                    @foreach($insideOrders as $index => $inside)
                         <tr>
                            <td>{{ $index + 1 }}</td>
                            <td>{{ $inside->menu->name }}</td>
                            <td>{{ $inside->menu->category->name }}</td>
                            <td>{{ $inside->order_amount }}</td>
                            <td>
                                @if($order->status=='1')
                                   <button id="send_order" class="btn btn-primary btn-xs"
                                     order_id="{{$order->order_id}}">ارسال<i id="send_icon"></i>
                                   </button>
                                     @else
                                      <button class="btn btn-success btn-xs" disabled>ارسال شده</button>

                                     @endif
                                   </td>
                              </tr>
                    @endforeach
                </tbody>                
         </table>

This is my js:- .

$('table tbody').on('click', 'button', function () {
        var order_id = $(this).attr("order_id");

        $.ajax({
            url: '{{route('sendOrders')}}',
            type: 'GET',
            dataType: 'json',
            data: {
                'id': order_id
            },
            success: function (response) {
                if (response) {
                    alert('ارسال شد!');
                    window.location.reload();
                }
                else {
                    alert('ارسال نشد!')
                }

            }, error: function (err) {


            }
        })

    });

I solved my problem, I put my table inside a dive like below, I don't know what happens when I put it inside a dive. but it works now.

<div id="apended_tb">
     <table class="table table-bordered">
                          
   <tbody>
     <?php
      $insideOrders = \App\OutsideModel::where('total_id', '=', $order->order_id)->get();
                            ?>
                @foreach($insideOrders as $index => $inside)
                     <tr>
                        <td>{{ $index + 1 }}</td>
                        <td>{{ $inside->menu->name }}</td>
                        <td>{{ $inside->menu->category->name }}</td>
                        <td>{{ $inside->order_amount }}</td>
                        <td>
                            @if($order->status=='1')
                               <button id="send_order" class="btn btn-primary btn-xs"
                                 order_id="{{$order->order_id}}">ارسال<i id="send_icon"></i>
                               </button>
                                 @else
                                  <button class="btn btn-success btn-xs" disabled>ارسال شده</button>

                                 @endif
                               </td>
                          </tr>
                @endforeach
            </tbody>                
     </table>
</div>

Then:-

 $('#apended_tb table tbody').on('click', 'button', function () {........}

Replace this

$('table tbody').on('click', 'button', function () {

with following:

$(document).on('click', '#send_order', function () {

And there is a mistake change order_id with

data-order_id

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