简体   繁体   English

jQuery重新加载DIV? (烧瓶/金贾2)

[英]JQuery to reload DIV? (Flask/Jinja2)

I've searched to no avail.. I guess my question is a bit unique. 我搜索无济于事。我想我的问题有点独特。 I have a DIV with some content in it, that content get's brought in currently by passing the object of a SQL query into the template. 我有一个DIV,其中包含一些内容,当前是通过将SQL查询的对象传递到模板中来引入该内容的。 That works fine, but doesn't allow me to update the content every x seconds, without refreshing the entire page. 效果很好,但不允许我每x秒更新一次内容,而无需刷新整个页面。 Something I don't want to do. 我不想做的事。 So I wanted to know I could force JQuery to reload my DIV element? 所以我想知道我可以强迫JQuery重新加载DIV元素吗? Would this also reload the code in it? 这还会重新加载其中的代码吗? Because that would force my div to update.. Here is the code. 因为那样会迫使我的div更新。这是代码。

<div id=content1>
    {{ set content = get_content(parm1, parm2) }} # This is a custom Jinja2 function I wrote to pull the content I need one in the template, but it only runs once.
    {% for row in content %}
    .. do stuff
    {% endfor %}
</div>

Is it possible to get JQuery to just reload that DIV with the code that is already there? 是否有可能让JQuery只是用已经存在的代码重新加载该DIV? If so that should fix my issue. 如果是这样,那应该可以解决我的问题。 I have thought about trying to create a call to get the content in JSON but IF possible would rather not. 我曾考虑过尝试创建一个调用以获取JSON中的内容,但如果可能的话,宁愿不这样做。

app.py app.py

@app.route('/')
def some_view():
    name = request.args.get('name', 'Anonymous')
    if request.is_xhr:
        template_name = 'template_ajax.html'
    else:
        template_name = 'template.html'
    return render_template(template_name, name=name)

template.html template.html

<html>
    <head>
        <title>Test</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <head>
    <body>
        <h3>Title</h3>
        <div>Hello, {{ name }}!</div>
        <p>Some text</p>

        <script>
            $(document).ready(function() {
                $('div').load('/?name=username');
            });        
        </script>
    </body>
</html>

template_ajax.html template_ajax.html

Hello from AJAX, {{ name }}!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM