javascript - 多次替换div的内容

标签 javascript html ajax jquery

我对我的服务器进行了选择,并且在所选服务器上加载信息而无需重新加载页面。我正在使用 ajax 和 ReplaceWith()。

我尝试使用 live() 多次替换信息,但只有效一次,为什么?

<script>
    $(function(){
        $('select').live('change', function(){
            $.ajax({
                type: "POST",
                url: "server.php",
                data: "hostname=" + $(this).val(),
                success: function(data){
                    $("#results").replaceWith(data);
                }
            })
        });
    });
</script>

最佳答案

这是因为您正在用数据替换#results 容器。下次 $("#results") 选择器将不会匹配任何元素(因为容器已被上一次调用替换)。

.html() 不会替换容器,而是更新容器的内容。

关于javascript - 多次替换div的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8077325/

相关文章:

JavaScript 使 AJAX 内容淡入

ajax - Tomcat 7 错误 : The requested resource (/TestSite) is not available. 即使我部署正确。为什么?

javascript - 如何使 jQuery 动画仅显示在 div 容器/横幅中

javascript - 如何保护 Django 模板以通过 Web 浏览器的检查元素功能更改其值(HTML 和 Javascript)?

javascript - 在 javascript 中用不同的数字替换数组元素

javascript - 如何在后台对mysql数据进行匹配和分类?

jquery - 如何在 jquery Mobile 中重新渲染动态生成的 anchor 标记?

javascript - react /归零 : get the user geolocation using a promise and dispatch the position to the action creator

html - 如何下载维基百科文章并存储在数据库中

php - 每次刷新页面时,行都会插入数据库