所以我有 2 页。 transfer.php 和 display_guide.php。我使用 AJAX 请求调用 display_guide.php 以在 tranfer.php 上显示它。 display_guide.php 上有一个简单的切换功能,当我单独测试时它工作得很好。但是,切换功能在使用 AJAX 调用的 transfer.php 上不起作用。任何帮助将不胜感激。
我看过其他文章,他们都建议我使用 .on 方法,我已经这样做了。一些附带信息:我正在使用 Bootstrap,但即使我注释掉 Bootstrap 链接,错误仍然存在——所以它不能是 Bootstrap 。
display_guide.php的代码如下:
<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>
但是,当我希望此切换反射(reflect)在 transfer.php 中时,我遇到了问题。内容加载但当我单击按钮时它拒绝切换。它不模仿独立页面 (display_guide.php) 的行为。
最佳答案
将切换代码移动到 transfer.php
文件中的 ajax 回调中,以便在附加 html 后将事件附加到对象。
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();
}
关于javascript - JQuery 无法处理 AJAX 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39242476/