ajax中的模态按钮事件

时间:2018-04-27 05:40:47

标签: javascript jquery html ajax

当我点击删除按钮时,我必须出现一个模态。

<div class="modal fade" id="confirm-modal" role="dialog">
    <div class="modal-dialog modal-md">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h3 class="confirm-modal-title">Confirmation Message</h3>
            </div>
            <div class="modal-body">
                <strong><p id="confirm-model-body"></p></strong>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-danger" id="confirm-model-yes-button" data-dismiss="modal">Yes</button>
                <button type="button" class="btn btn-default" id="confirm-model-no-button" data-dismiss="modal">No</button>
            </div>
        </div>
    </div>
</div>

我只在yes模式按钮上创建AJAX事件,当我单击没有模态按钮或X关闭没有任何操作完成但第二次当我单击同一行的删除按钮并单击是时行将删除但是它还得到这条消息alert(data.message)为什么?如果没有make事件或X关闭?

,我如何防止这种情况发生?
$(document).on('click', '.delete_librarian', function(){
    var librarianDataRow=$(this).closest('tr');
    var librarianId=parseInt(librarianDataRow.find('td:eq(0)').text());
    var librarianName=librarianDataRow.find('td:eq(1)').text();
    $('#confirm-model-body').text("Are you sure you want to delete "+librarianName+" data");
    $('#confirm-modal').modal({backdrop: 'static'});
    $('#confirm-model-yes-button').click(function () {
        $.ajax({
            url: "/api/admin/librarian",
            type: "DELETE",
            dataType: "json",
            data: JSON.stringify({id:librarianId}),
            success:function (data) {
                if(data.success == true)
                {
                    window.location.reload();
                }
                else
                {
                    alert(data.message); //this message appear even when yes button after click no button or x close .
                }
            }
        });
    });
});

1 个答案:

答案 0 :(得分:0)

我不确定你在说什么,但我认为应该通过在ajax调用中替换成功语句来解决你的问题:

    success:function (data,textStatus) {
        if(textStatus == 'success')
        {
            window.location.reload();
        }
        else
        {
            alert(data.message); //this message appear even when yes button after click no button or x close .
        }

或在服务器中发送成功消息,如(在php中):

$data['success'] = true;

echo json_encode($data);