【问题标题】:JavaScript doesn't work after ajax loading (using table)ajax加载后JavaScript不起作用(使用表)
【发布时间】:2020-05-21 06:15:27
【问题描述】:

我有一个由数据库使用 php 自动生成的四行表...最后一行 (4) 有一个按钮来触发下面的函数...它对每一行重复。

单击按钮后...脚本发送更新数据库的请求以将状态“挂起”切换为“已取消”。

此时一切都很好......但是......当我用表格“$('#lista').load(location.href +'#lista> *');”刷新div时

脚本在加载后第二次不起作用。 (div 更新没问题,但只能工作一次)

我需要脚本在第一次刷新后工作......每次我点击行按钮......

我认为这可能是 ajax 请求和 javascript 不兼容。

如果你能帮助我,我真的很感激。 谢谢大家。

<script>

var index, table = document.getElementById('table');
            for(var i = 1; i < table.rows.length; i++)
            {
                table.rows[i].cells[4].onclick = function()
                { //inicio da função
                    var c = confirm('Você quer cancelar essa aula?');
                    if(c === true)
                    {//inicio confirmador
                      var table = document.getElementById('table');
                        index = this.parentElement.rowIndex;
                        value_index_id= table.rows[index].cells[0].innerHTML;
                        var id = value_index_id;
                        $.ajax({
                  type: 'GET',
                  url: 'cancelar_aulas.php',
                  data: {id_geral:id},
                  success: function(data){
                    alert(data);
                    $( '#lista' ).load(location.href + ' #lista > *' );

                  }
              })

                    } //fim do confirmador

                    //validador
                    //console.log(index);
                    //console.log (value_index_id);
                    console.log(id);

                };//fim da função

            }
</script>";

【问题讨论】:

  • 您在开始时获得表长度(可能是在页面加载时),但是当您获得新数据时您不会更新它。我敢打赌 onclick 事件处理程序根本没有附加到新行。
  • 不...它不再触发
  • 如果我不加载 div .... 只需刷新所有页面...效果很好...但我需要一些动态的东西...此代码有效...但如果我不刷新。 ..我不能再这样做了
  • 我不知道...我猜我必须使用...jquery live() 或delegate() ...我不知道该怎么做...
  • 您可能没有将侦听器附加到新元素上。如果是这种情况,您需要查看委托事件。基本上不是监听元素本身,而是监听容器内的元素发生的事件。

标签: javascript jquery ajax


【解决方案1】:

您只是在页面加载时将 onclick 事件处理程序附加到表格。这就是为什么它只适用于第一次点击。当您向表中添加新数据时,您需要执行一些操作以将 onclick 事件添加到新行中。

有几种不同的方法可以做到这一点:

  • jQuery 可以像这样将事件附加到动态创建的元素上:

    $("table").on("click", "table tr", function() {
        // Note that this event would fire for ALL rows, 
        // but it will automatically attach this event to new rows
        // that are added after page load
        // This would work, you just would have to check if the clicked
        // row is one of the last 4.
    }
    
  • 创建自己的函数来附加事件,每次添加新行时都会运行该函数。比如:

    function AttachClick() {
        let table = document.getElementById("table");
        let position = table.rows.length;
        while (position > table.rows.length - 4) {
            table.rows[position].addEventListener("click", function() {
                // Do your on click here
                // On success, you would fire this "AttachClick()" function
            }
            position--;
        }
    }
    

注意while 循环从表的末尾而不是开头开始。如果您有一个包含很多行的表,那么从末尾开始迭代可能会更快(因为这些是您唯一关心的行)。

【讨论】:

  • 函数“this.parentElement.rowIndex”会自动检测并获取我需要的信息...但加载后... onclick 停止工作,不再触发该功能...我不要创建新行...只是重新加载新信息...就像脚本不再检测元素...我必须重新加载脚本?
  • 我在没有 "$( '#lista' ).load(location.href + ' #lista > *' ); 的情况下进行了测试...正常工作,所有行都触发该功能...但不刷新表格...当我刷新页面时,我单击的所有行都更改并更新...当我使用 $('#lista') .load(location.href + '#lista > *'); ...该功能仅在第一次尝试时有效
  • 问题和解决方法应该还是一样的。当您将新数据加载到表中时,它可能会破坏现有行并重新创建它们。这意味着事件处理程序丢失。您可以检查这是开发人员控制台(在加载数据后查看行是否有事件)。加载新数据后,您仍需要重新附加事件处理程序。
猜你喜欢
  • 1970-01-01
  • 2011-05-27
  • 2015-11-17
  • 1970-01-01
  • 1970-01-01
  • 2012-06-08
  • 1970-01-01
  • 1970-01-01
  • 2015-12-11
相关资源
最近更新 更多