繁体   English   中英

在flask中返回url_for为false

[英]return false with url_for in flask

<!DOCTYPE html>
<html>
    <head>
        <title>Vote</title>

        <script>
            document.addEventListener('DOMContentLoaded',()=>{
                document.querySelector('button').onclick=()=>{
                    return false;
                }
            });
        </script>
    </head>
    <body>            
        <button onclick="{{ url_for('chat') }}">hi</button>      
    </body>
</html>

这似乎不起作用

我也尝试过:

<button onclick="{{ url_for('chat') }} ; return false;">hi</button> 

和:

<input type="button" id="submit" onclick="{{ url_for('chat') }} ; return false;" >

但是,这给了我控制台错误。

单击按钮时如何停止页面加载? 可以在<script>标记中使用url_for()吗?

url_for()所做的所有url_for()都是在烧瓶路由中返回端点的字符串。

例如,如果您有一条路线:

@app.route('/some_route')
def some_route():
    # do something
    return render_template('template.html')

{{url_for('some_route')}}将返回字符串'/some_route'

onclick html属性需要一个javascript函数,而不是路由的字符串。 您可以执行以下操作:

<!DOCTYPE html>
<html>
    <head>
        <title>Vote</title>

        <script>
            $(function(){
            var someFunction = function(){
                 // perform actions
                }

            })

        </script>


    </head>
    <body>

<button onclick="someFunction()">hi</button>      


    </body>
</html>

暂无
暂无

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

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