【问题标题】:Loading a new page without refreshing the current page加载新页面而不刷新当前页面
【发布时间】:2020-04-03 09:21:12
【问题描述】:

这是我的代码.... 好的.html

{% extends "sch/base.html" %}
{% load staticfiles %}
{% block content %}
<div class="row" id="ada">
    <form action="" method="post">
       {% csrf_token %}
        <div align="center" class="container table-responsive mt-2" >
{% block scripts %}
{{ block.super }}
<script>
window.onload = function()
    {
        var button = document.querySelector('button');
        button.onclick= function(e)
            {
                e.preventDefault();
                addRecord()
            }
    }
    function addRecord() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("ada").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "{% url 'add-advert' %}", true);
  xhttp.send();
}
</script>
{% endblock %}
            <div> <a href="javascript:;" onclick="addRecord()" ><button type="button" class="btn btn-primary"> Add New Advert </button></a></div>
    </form>
</div>
{% endblock %}

base.html

<!DOCTYPE html >
{%load staticfiles%}
{% load crispy_forms_tags %}
<html>
<head>
    <meta charset="UTF-8">
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <link rel="stylesheet" href="{% static 'style.css' %}"/>
    <title>{% block title %}{% endblock %}</title>
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

</head>
<body class="container-fluid">
<div class="row">
<div class="col-lg-12">

    {% block content %}
    {% endblock %}

</div>
</div>
</body>
</html>

我是 javascript 新手.... 我正在尝试在当前页面(okay.html)的 div 中加载另一个页面(广告)。它们都扩展了 base.html。我也尝试使用 $.ajax,但它没有用。 我被告知要添加第一个函数脚本,因为最初我得到了未捕获的引用,所以我试图寻找一种更好的方法来定义函数 addrecord

【问题讨论】:

    标签: javascript jquery html ajax django-templates


    【解决方案1】:

    我想也许可以尝试在这里添加location.reload();

        if (this.readyState == 4 && this.status == 200) {
          document.getElementById("ada").innerHTML = this.responseText;
          location.reload();
        }
      };
    

    【讨论】:

    【解决方案2】:

    HTML

    <div class="row">
            <nav role="navigation" class="nav">
                <a class="link1">Button</a>
           </nav>
    </div>
    

    阿贾克斯

    <script>
    jQuery( ".link1" ).on( "click", function(e) {
        e.preventDefault();
        processAjaxData('about', '/home/about', 'about');
    }
    </script>
    

    【讨论】:

      【解决方案3】:

      尝试使用 iframe 而不是 DIV。加载第二页

      【讨论】:

      • 欢迎来到 Stack Overflow!请阅读How to Answeredit 你的答案。为什么这解决了 OP 面临的问题,他们应该如何“加载他们的第二页”以及这个答案到底是什么解决了其他人没有解决的问题?此类答案更适合作为 cmets,只要您没有足够的声誉发表评论,请不要回答此类内容(即没有对解决方案进行深入解释)。谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-29
      • 2012-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多