onclick事件仅适用于第一个孩子(laravel foreach)

时间:2017-01-01 12:50:31

标签: javascript html ajax laravel-5

我对表格行中的每个数据进行按钮操作。每一行都使用foreach laravel刀片模板生成。该事件将触发Javascript操作以删除数据。但就我而言,该按钮仅适用于第一行数据。 这里我的 Javascript html 代码:



function hapusFaskes(kode){
        var kodeStr = kode.toString();
        var mes = confirm('Yakin Faskesnya mau di Hapus?');
        alert(kode);
        if (mes == true){
            $.ajax({
                headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                url:'{{url('deleteFaskes')}}'+'/'+kode,
                type:'get',
                success: function (r){
                    console.log(r);
                    alert(r);
                    location.reload();
                }
        });
        }
    }

<tbody id="hasil-tabel">
  @foreach ($faskes as $val)
  <tr>
     <td>{{$val->kode}}</td>
     <td>{{$val->nama}}</td>
     <td>{{$val->jenis}}</td>
     <td>{{$val->alamat}}</td>
     <td>{{$val->notelp}}</td>
     <td>{{$val->jambuka}}</td>
     <td>
         <a href="{{url('editfaskes').'/'.$val->kode}}" class="btn btn-default" >Edit</a>
         <a href="#" type="button" onclick="hapusFaskes({{$val->kode}})" class="btn btn-default" data-toggle="modal" data-target="#modal-faskes_delete">Hapus</a>
     </td>
  </tr>
  @endforeach
</tbody>
&#13;
&#13;
&#13;

我不知道我的代码是否有问题,如果您知道错误,请通知我,如果您对我的代码有误,那将对我有很大帮助。感谢

1 个答案:

答案 0 :(得分:0)

尝试使用以下代码与表单操作以及甜蜜警报进行确认。

<form action="{{ url('deleteFaskes/'. kode) }}" method="POST" class="btn-inblock-laravel">
{!! method_field('delete') !!}
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<a class="btn btn-danger btn-icon btn-xs tip" type="button" onclick="commonDelete(this)"><i class="icon-remove3"></i></a>
</form>

// include sweet alert js in assets folder
<script src="{{ asset('assets/js/sweetalert.min.js') }}"></script>
<script>
    function commonDelete(element) {
        swal({
                    title: "Are you sure?",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "Yes, delete it!",
                    closeOnConfirm: false
                },
                function () {
                    element.parentElement.submit();
                    setTimeout(function () {
                        swal({
                            title: "Deleted!",
                            text: "",
                            type: "success",
                            showConfirmButton: false
                        });
                    }, 2000);
                });
    }
</script>