简体   繁体   English

Flask WTForms 中带有 Javascript 的装载程序

[英]Loader with Javascript in Flask WTForms

I am attempting to include a loading icon with Javascript in my Flask App that uses WTForms.我试图在使用 WTForms 的 Flask 应用程序中包含一个带有 Javascript 的加载图标。 I have the loader set up as an onclick event that sets the loader to being shown upon button click.我将加载程序设置为onclick事件,该事件将加载程序设置为在单击按钮时显示。 The problem is, it shows the loader upon click even if the Form is not validated (IE has nothing in it).问题是,即使表单未经过验证(IE 中没有任何内容),它也会在单击时显示加载程序。

Normally I would just check the form like:通常我只会检查以下表格:

HTML: HTML:

<input type="submit" class="btn btn-outline-info" name="submit_button" value="Retrieve Data" onclick="test1()">
<div id='loader' class="loader"></div>

JS: JS:

window.onload = function () {

         document.getElementById("loader").style.display = "none";
}
function test1() {
var user_input = document.getElementById('form').value;
if (user_input) {
    document.getElementById("loader").style.display = "block";
}
}

How can I check the value of a WTForm using Javascript, WITHOUT resorting to waiting for the data to come all the way through the Flask API (no point in having a loader if it only shows up as the data comes through anyway)?如何使用 Javascript 检查 WTForm 的值,而不是一直等待数据通过 Flask API

This is my WTForms code:这是我的 WTForms 代码:

<div class="form-group">
{{ form.service.label(class="form-control-label") }} {% if form.service.errors %} {{ 
form.service(class="form-control form-control-lg
is-invalid")}}
<div class="invalid-feedback">
{% for errors in form.service.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %} {{ form.service(class="form-control form-control-lg") }} {% endif %}
</div>

As far as i understand it, you're trying to check if there is a value in an input field, which is inside a form.据我了解,您正在尝试检查表单内的输入字段中是否有值。

I interpret this line:我解释这一行:

document.getElementById('form').value;

like you give it the id of the form and not the input field you are checking.就像你给它表单的 id 而不是你正在检查的输入字段。 And you can't do that because, it will always return false.你不能这样做,因为它总是会返回 false。

What you instead need to do is get the value from the specific input field, and not the whole form.相反,您需要做的是从特定输入字段中获取值,而不是整个表单。

That means inserting the id of the input field you are checking, into the document.getElementById('insert input field id');这意味着将您正在检查的输入字段的 id 插入到document.getElementById('insert input field id');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM