在Laravel视图中,Document.getElementById(...)为Null

时间:2018-12-01 15:20:34

标签: javascript laravel laravel-blade

控制台中的消息

  

TypeError:document.getElementById(...)为空。

刀片(包含要删除的按钮)

<td>
    <a href="#" class="btn btn-primary btn-sm"><i class="fa fa-pencil"></i></a>
    <button type="button" class="btn btn-danger btn-sm"
            onclick="deleteSaleItem({{ $sale->id }})">
        i class="fa fa-trash"></i>
    </button>
    <form id="delete-form-{{ $sale->id }}" action="{{ route('secretary.sales.destroy.item',$sale->id) }}" method="POST"
          style="display:none;">
        @csrf
        @method('DELETE')
    </form>
</td>

JavaScript实现

<script>

    function deleteSaleItem(id){
        const swalWithBootstrapButtons = swal.mixin({
        confirmButtonClass: 'btn btn-success',
        cancelButtonClass: 'btn btn-danger',
        buttonsStyling: false,
        })

        swalWithBootstrapButtons({
        title: 'Are you sure?',
        text: "You won't be able to revert this!",
        type: 'warning',
        showCancelButton: true,
        confirmButtonText: 'Yes, delete it!',
        cancelButtonText: 'No, cancel!',
        reverseButtons: true
        }).then((result) => {
        if (result.value) {
            event.preventDefault();
            document.getElementById('delete-form-'+id).submit();
        } else if (
            // Read more about handling dismissals
            result.dismiss === swal.DismissReason.cancel
        ) {
            swalWithBootstrapButtons(
            'Cancelled',
            'Your data is safe :)',
            'error'
            )
        }
        })
    }
</script>

我需要知道这段代码出了什么问题,导致null。谢谢。

1 个答案:

答案 0 :(得分:1)

您的代码中只有一个getElementById。

integer status, fd;
reg [4:0] value;

initial begin
   fd = $fopen("data_file.dat", "r");
   if (!fd) $error("could not read file");
   while (!$feof(fd)) begin
         status = $fscanf(fd,"%b",value);
         // check status, then do what you need to do with value
   end
end

因此,它可能是'id'变量为空或null,而您检索的元素变为null。

在document.getElementById代码之前尝试调试或console.log您的“ id”变量,以查看变量发生了什么。