我不知道为什么每次刷新都要执行"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>发布于 2021-06-09 11:26:49
您将立即调用这些函数,因为您已经将它们封装在了花括号中。这意味着,代码将被计算为并立即执行。
<button onclick="{{ create_experiment() }}">Create new experiment</button>如果要调用单击时的函数,请删除括号,并将调用添加为字符串文本。
<button onclick="create_experiment()">Create new experiment</button>在调用set_active_experiment()时也是如此。
https://stackoverflow.com/questions/67903045
复制相似问题