簡體   English   中英

在燒瓶中帶有onclick的url_for

[英]url_for with onclick in flask

from flask import Flask,render_template,url_for

app = Flask(__name__)

@app.route("/")
def index():
    return render_template("index.html")


@app.route("/okay")
def okay():
    return render_template("okay.html")



if __name__ == '__main__':
    app.run(debug=True)

這是index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

</head>
<body>
    <h1>index</h1>
    <h2 onclick="{{ url_for('okay') }}">click me</h2>
    <h2><a href="{{ url_for('okay') }}">click me</a></h2>
</body>
</html>

單擊我這不起作用,而且表單操作url_for不起作用

更改路由並使用socketio發送消息,但是socketio正在工作

如何解決這個問題?

在HTML元素上使用onclick通常會導致對腳本函數的調用,例如onclick=myFunction()

<body>
    <h1>index</h1>
    <h2 id="foo" onclick="myFunction();">click me</h2>
    <h2><a href="{{ url_for('okay') }}">click me</a></h2>
</body>

您需要為onclick=myFunction();添加一些Javascript代碼onclick=myFunction(); 上班。

function myFunction() {

    $.ajax({
        url: '/okay',
        data: JSON.stringify(data),
        method: 'POST',
        contentType: 'application/json',
        success: function(data) {
            $('#foo').replaceWith(data);
        }
    });
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM