dtz46697 2016-08-31 06:43
浏览 62
已采纳

JQuery没有处理AJAX请求

So I have 2 pages. transfer.php and display_guide.php. I call display_guide.php using an AJAX Request to display it on tranfer.php. There is a simple toggle function on display_guide.php and it works just fine when I test it alone. However the toggle function does not work when on transfer.php using the AJAX Call. Any help would be greatly appreciated.

I have looked on other articles and they all suggested that I use the .on method and I have done so. Some side information: I am using Bootstrap, but even when I comment out the bootstrap link, the error persists -- so it can't be bootstrap.

The code for display_guide.php is as follows:

<body>
<!-- GENED TABLE WITH CATEGORY IMPLEMENTATION----------------------------------->

<button id="sri">Toggle between hiding and showing the paragraphs</button>
<p>This is another small paragraph.</p>

<script>
    $(document).on("click", "button", function(){
        $("p").toggle();
    });
</script>
</body>

However, when I want this toggle to be reflected in transfer.php, I have issues. The content loads but it refuses to toggle when I click on the button. It does not mimic the behavior of the standalone page (display_guide.php).

  • 写回答

1条回答 默认 最新

  • doushi1473 2016-08-31 06:47
    关注

    Move your toggle code inside ajax callback inside transfer.php file, to attach event to object after the html has been appended.

          function loadTransferGuide(str3) {
            var xhttp3 = new XMLHttpRequest();
            xhttp3.onreadystatechange = function() {
                if (xhttp3.readyState == 4 && xhttp3.status == 200) {
                    document.getElementById("transfer_guide").innerHTML = xhttp3.responseText;
                    $(document).on("click", "button", function(){
                         $("p").toggle();
                    });
                }
            };
            xhttp3.open("GET", "display_guide.php", true);
            xhttp3.send();
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵