ajax请求只能在laravel中使用一次

时间:2016-08-14 19:09:27

标签: javascript jquery ajax laravel-5

起初我的ajax请求工作正常,但是在用ajax请求加载部分页面后,ajax停止工作(不仅是ajax,而是对元素的整个操作),为什么?

我的HTML代码:

<td>

                    @foreach($file_defects[$revision->revision_id] as $file_defect)
                    <a href='http://localhost/sponaDP/laravel/public/docs/revision_intervals/{{ $revision->rev_interval_id . '/' . $revision->date_of_revision->format('d.m.Y') . '/defects/' .
                                   $file_defect->getFilename() }}' target="_blank">
                        {{$file_defect->getFilename()}}
                    </a>
                    <a class="destroyFileFromRevision" id="{{$file_defect->getPathname()}}"  style='color:red;'><span style="display:none;" class="rev">{{$revision->revision_id}}</span><span class='glyphicon glyphicon-remove'></span></a>
                    <br>
                    @endforeach
</td>

我的阿贾克斯:

$(".destroyFileFromRevision").click(function () {
    var filename = this.id;
    var rev = $(this).children('.rev').text();
    $.ajax({
        type: "POST",
        url: "http://localhost/sponaDP/laravel/public/revisions/destroy-file",
        data: 'filename=' + filename + '&rev=' +rev,
        beforeSend: function () {
            $(".destroyFileFromRevision").parent('td').html(
                    "<i class='fa fa-spinner fa-spin' aria-hidden='true' \n\
                    ></i>");
        },
        success: function (data) {
            $(".fa").parent('td').html(data);
        }
    }, 'json');
});

这些是ajax请求返回的html数据(url:... / destroy-file):

@foreach($file_defects[$revision->revision_id] as $file_defect)
<a href='http://localhost/sponaDP/laravel/public/docs/revision_intervals/{{      $revision->rev_interval_id . '/' . $revision->date_of_revision->format('d.m.Y') . '/defects/' . $file_defect->getFilename() }}'  target="_blank">
{{$file_defect->getFilename()}}
</a>
<a class="destroyFileFromRevision" id="{{$file_defect->getPathname()}}"       style='color:red;'><span class='glyphicon glyphicon-remove'></span></a>
<br>
@endforeach

1 个答案:

答案 0 :(得分:2)

你必须通过这种方式使元素永远存在     $(".destroyFileFromRevision").click(function ()

    $(document).on('click', '.destroyFileFromRevision', function()