[英]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.