为什么单击按钮时 JQuery 代码不起作用?

时间:2021-05-06 08:01:29

标签: javascript jquery

当我点击按钮时我的 JQuery 代码有什么问题,它不起作用,我搜索了很多并尝试了很多方法,但我的问题没有解决。我在其他文件中使用了此代码,在那里它可以正常工作,但在此文件中不起作用。

这是我的桌子:

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

这是我的 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) {


            }
        })

    });

2 个答案:

答案 0 :(得分:0)

我解决了我的问题,我把我的桌子放在下面的潜水中,我不知道当我把它放在潜水中时会发生什么。但现在可以用了。

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

然后:-

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

答案 1 :(得分:-1)

替换这个

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

以下内容:

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

并且有一个错误更改 order_id 与

data-order_id