【问题标题】:Reload Table (NOT Datatable()) after jQuery/Ajax successjQuery/Ajax 成功后重新加载表(不是 Datatable())
【发布时间】:2020-06-04 19:53:08
【问题描述】:

我知道这个话题已经被多次讨论过,因为我在发布之前已经搜索并尝试了很多选项,所以请不要关闭它。

我有一个表格,显示我的 mysql 数据库中的班次条目。在我使用 jQuery 删除一行后,我希望在成功时刷新总计行,或者整个表本身。

我遇到的问题是删除和重新加载过程有效,但它只能工作一次而不是多次。

谁能帮忙?

我的桌子:

<table id="cardTable">
  <tr>
    <td>Mon</td>
    <td>11:00 hrs</td>
    <td>£185.00</td>
  </tr>
  <tr>
    <td>Tue</td>
    <td>11:00 hrs</td>
    <td>£100.00</td>
  </tr>
  <tr>
    <td>Wed</td>
    <td>11:00 hrs</td>
    <td>£100.00</td>
  </tr>
  <tr>
    <th>Totals:</th>
    <th>33:00 hrs</th>
    <th>£300.00</th>
  </tr>
</table>

jQuery:

$(document).ready(function(){

    // Delete 
    $('.delete').click(function(){

        var el = this;
        var id = this.id;
        var splitid = id.split("_");

        // Delete id
        var deleteid = splitid[1];

        // AJAX Request
        $.ajax({
            url: 'system/actions/deleteShift.php',
            type: 'POST',
            data: { id:deleteid },
            success: function(response){

                if(response == 1){
                    // Remove row from HTML Table
                    $(el).closest('tr').fadeOut(600,function(){
                        $(this).remove();
                    });
                    $("#cardTable").reload(" #cardTable");
                }
                else {
                    alert('Invalid ID.');
                }

            }
        });

    });

});

【问题讨论】:

  • 试试看$('.delete').on(function(){
  • ^^^ 我的第一个想法也是
  • 所以将 $('.delete').click(function(){ 替换为 $('.delete').on(function(){ ??
  • 是的。第一次删除后,DOM 发生了变化,因此您可能需要考虑如何绑定到动态元素。在这里阅读更多:stackoverflow.com/questions/203198/…

标签: php jquery mysql ajax


【解决方案1】:

我不确定它是否漂亮,您可以尝试将所有内容放在一个函数中,并在成功完成 ajax 后重新初始化它。

$(document).ready(function(){

    initClicks();
    function initClicks() {
        // Delete 
        $('.delete').unbind().click(function(){

            var el = this;
            var id = this.id;
            var splitid = id.split("_");

            // Delete id
            var deleteid = splitid[1];

            // AJAX Request
            $.ajax({
                url: 'system/actions/deleteShift.php',
                type: 'POST',
                data: { id:deleteid },
                success: function(response){

                    if(response == 1){
                        // Remove row from HTML Table
                        $(el).closest('tr').fadeOut(600,function(){
                            $(this).remove();
                        });
                        $("#cardTable").reload(" #cardTable");
                        initClicks();
                    }
                    else {
                        alert('Invalid ID.');
                    }

                }
            });

        });
    }
});

【讨论】:

  • 感谢@Beryth,我已经尝试过这样做,但不幸的是它不起作用。我是 JS 的新手,所以我很难把它拼凑起来。
猜你喜欢
  • 1970-01-01
  • 2016-12-17
  • 2019-11-20
  • 1970-01-01
  • 2019-10-02
  • 1970-01-01
  • 2017-06-04
  • 2011-12-14
  • 1970-01-01
相关资源
最近更新 更多