简体   繁体   English

jQuery .on在ajax删除和div刷新后不起作用

[英]jQuery .on not working after ajax delete and div refresh

On a page with a tab control, each tab contains a table, each tr contains a td with a button which has a value assigned to it. 在带有选项卡控件的页面上,每个选项卡都包含一个表,每个tr包含一个带有按钮的td,该按钮具有为其分配值。

<td>
 <button type="button" class="btn" name="deleteEventBtn" value="1">Delete</button>
</td>

This code below works for the first delete. 以下代码适用于第一次删除。 After the AJAX call & the refresh of the div, no further delete buttons can be clicked. 在AJAX调用和div刷新后,无法单击其他删除按钮。 The .on is attached to the document. .on附加到文档中。 The same happens if I attach it to the body or anything closer to the buttons. 如果将其连接到主体或按钮附近的任何东西,也会发生相同的情况。

function deleteRecord(url, id, container) {
    $.ajax({
        type: "POST",
        url: url,
        data: { id: id },
        success: function (data) {         
            $('#delete-popup').hide();
            $(container).trigger('refresh');
        }
    }); 
}

$(document).ready(function () {
        $(document).on('click', '[name^="delete"]', function (e) {
            e.preventDefault();
            var id = $(this).val();
            $('#current-record-id').val(id);
            $('#delete-popup').modal('show');
        });

        $('#delete-btn-yes').on('click', function (e) {
            e.preventDefault();          
            var recordId = $('#current-record-id').val();
            var recordType = location.hash;
            switch (recordType) {
                case "#personList":
                    deleteRecord(url, recordId, recordType);
            break;
            }
        });
});

Any ideas? 有任何想法吗? Could it be related to the wildcard for starts with [name^="delete"]? 可能与以[name ^ =“ delete”]开头的通配符相关吗? There are no other elements where the name starts with 'delete'. 名称没有其他以“ delete”开头的元素。

EDIT 编辑

When replacing 更换时

$(container).trigger('refresh'); 

with

location.reload();

it "works", however that refreshes the whole page, loses the users position and defeats the point of using AJAX. 它“起作用”,但是刷新了整个页面,失去了用户位置,使使用AJAX失去了意义。

As the button click is firing at first attempt, there is no issue in that code. 由于按钮单击是第一次触发,因此该代码中没有问题。 All you have to do is, put the button click event in a method and call it after the refresh. 您所要做的就是将按钮单击事件放入方法中,并在刷新后调用它。 This way, the events will be attached to the element again. 这样,事件将再次附加到元素。 See the code below, 参见下面的代码,

function deleteRecord(url, id, container) {
    $.ajax({
        type: "POST",
        url: url,
        data: { id: id },
        success: function (data) {         
            $('#delete-popup').hide();
            $(container).trigger('refresh');
          BindEvents();
        }
    }); 
}

$(document).ready(function () {
        BindEvents();
});

function BindEvents()
{
  $(document).on('click', '[name^="delete"]', function (e) {
            e.preventDefault();
            var id = $(this).val();
            $('#current-record-id').val(id);
            $('#delete-popup').modal('show');
        });

        $('#delete-btn-yes').on('click', function (e) {
            e.preventDefault();          
            var recordId = $('#current-record-id').val();
            var recordType = location.hash;
            switch (recordType) {
                case "#personList":
                    deleteRecord(url, recordId, recordType);
            break;
        });
  }

Apologies to all and thanks for your answers. 向所有人致歉,并感谢您的回答。 The problem was due to the way the popup was being shown & hidden. 问题是由于弹出窗口的显示和隐藏方式所致。

$('#delete-popup').modal('show');

and

$('#delete-popup').hide();

When I changed this line to: 当我将此行更改为:

$('#delete-popup').modal('hide');

it worked. 有效。 Thanks to LShetty, the alert (in the right place) did help! 感谢LShetty,警报(在正确的位置)确实提供了帮助!

If you are using Bootstrap Modal After Ajax Request before Refreshing page add 如果您使用的是Ajax之后的Bootstrap Modal,请在刷新之前添加页面

$('.modal').modal('hide');

This Line will Close your Modal and reload your page. 该行将关闭您的模式并重新加载您的页面。 Before that it will complete all Ajax Request things. 在此之前,它将完成所有Ajax Request事务。

But for google chrome there is no issues :) hope this help someone. 但对于谷歌浏览器没有问题:)希望这对某人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM