【问题标题】:HTML: "onclick" does action every refreshHTML:“onclick”每次刷新都会执行操作
【发布时间】:2021-06-09 11:13:08
【问题描述】:

我不知道,为什么每次刷新都会执行“onclick”操作(在按钮上或 - 我都尝试过)。当我打开这个页面时,我可以观察到与“set_active_experiment()”和“create_experiment()”相关的动作。我只想在单击链接/按钮时调用这些函数。你有什么想法吗?

代码:

<html>
<head>
    <title>Experiments</title>
</head>

<body>
    <div class="experiments">
        <h3>Your experiments:</h3>
        {% with experiments = get_user_experiments() %}
            {% if experiments %}
                    {% for exp in experiments %}
                        <a href="{{ url_for('experiment') }}"><button onclick="{{ set_active_experiment(exp) }}">{{ exp }}</button></a>
                    {% endfor %}
            {% endif %}
        {% endwith %}
    </div>
    <div>
        <a href="{{ url_for('experiments') }}"><button onclick="{{ create_experiment() }}">Create new experiment</button></a>
    </div>
</body>

【问题讨论】:

  • 请添加你的JS函数。

标签: html flask button onclick jinja2


【解决方案1】:

您正在立即调用这些函数,因为您已将它们包裹在大括号中。这意味着,代码被评估并立即执行。

<button onclick="{{ create_experiment() }}">Create new experiment</button>

如果您想在单击时调用函数,请删除括号并将调用添加为作为字符串文字

<button onclick="create_experiment()">Create new experiment</button>

调用set_active_experiment()时也是如此。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-29
    • 2011-07-06
    • 1970-01-01
    • 2014-01-15
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多