You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何替换JavaScript函数中的alert实现Flask路由跳转?

实现JavaScript跳转Flask路由的方法

基础跳转(不带参数)

直接替换原dateClick函数中的alert语句,用window.location.href跳转到Flask的/addPage路由即可:

dateClick: function(info) {
    // 跳转到Flask的addPage路由
    window.location.href = '/addPage';
}

带参数跳转(传递日期和资源ID)

如果需要把点击的日期和资源ID传递给Flask页面,可按以下步骤修改:

  1. 修改Flask路由,接收URL参数:
from flask import request

@app.route('/addPage')
def addPage():
    # 获取URL中的参数
    selected_date = request.args.get('date')
    resource_id = request.args.get('resource_id')
    # 将参数传递给模板供页面使用
    return render_template('manual_entry.html', date=selected_date, resource_id=resource_id)
  1. 修改JavaScript函数,拼接带参数的URL:
dateClick: function(info) {
    // 对参数编码,避免特殊字符导致URL解析错误
    const encodedDate = encodeURIComponent(info.dateStr);
    const encodedResourceId = encodeURIComponent(info.resource.id);
    // 拼接目标URL
    const targetUrl = `/addPage?date=${encodedDate}&resource_id=${encodedResourceId}`;
    // 执行跳转
    window.location.href = targetUrl;
}

跳转方式说明

  • window.location.href:普通跳转,用户可通过浏览器返回按钮回到原页面
  • 若不需要保留原页面历史,可使用window.location.replace(targetUrl),替换当前页面的历史记录

内容的提问来源于stack exchange,提问作者Mt Khalifa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 17:53:14