Ajax请求正在处理第一个元素,但不能处理其他请求?

时间:2018-09-12 21:20:05

标签: javascript php jquery ajax

我正在通过AJAX实现删除功能,但是当我从表中点击删除图标时,第一个元素删除请求可以正常工作,并且在第一次单击时它会删除记录,但不会发送第二个或第三个元素的删除请求

JS Ajax请求:

// Delete symptom which is linked with a Remedy ( Causes page )
    $("#linked-symptom").click(function(e) {
        e.preventDefault();

        var symptom_id = $("#linked-symptom").attr('data-id');
        var dataString = '&id='+symptom_id;

        $.ajax({
            type: 'POST',
            data: dataString,
            url: '<?php echo $this->CxHelper->Route('eb-admin-delete-linked-symptom') ?>',
            success: function(data) {
                $("#successMessage").show().delay(5000).fadeOut();
            }
        });
    });

HTML标记:

<table id="cx-records-table" class="table display table-striped table-bordered" width="100%">
            <thead>
                <tr>
                    <th>
                        Title
                    </th>
                    <th>
                        Delete
                    </th>
                </tr>
                <?php foreach($symptoms as $key => $symptom){ ?>
                    <tr>
                        <td class="all"><?php echo $symptom['title']; ?><br></td>
                        <td><a class="cx-action row-delete" href="javascript:void(0)" data-id="{{symptom['id']}}" id="linked-symptom"><i class="fa fa-trash-o"></i></a></td>
                    </tr>
                <?php } ?>

            </thead>
            <tbody></tbody>
        </table>

功能

public function deldeleteLinkedSymptomAction(){

        // Get the Evaluation Symptom Id (if any)
        $symptomId = $this->request->get('id', null);

        $symptom = CxEbEvaluationSymptomCause::getLinkedSymptomEntryForDeletion($symptomId);
        if( $symptom ){
            $symptom->delete();
            return true;
        }else{
            return false;
        }

    }

1 个答案:

答案 0 :(得分:0)

HTML ID在整个页面上应该是唯一的,但是您有多个实例,然后尝试将它们用于jQuery操作。它只会影响找到的第一个。尝试改用this

// Delete symptom which is linked with a Remedy ( Causes page )
$("a.row-delete").click(function(e) {
    e.preventDefault();

    var symptom_id = $(this).data("id");
    var dataString = '&id='+symptom_id;
    ...
});