简体   繁体   English

Flask-WTForms 使用 jQuery 移动到下一个表单字段

[英]Flask-WTForms Using jQuery to Move to Next Form Field

I have a jQuery script that auto-moves the focus down a column of input fields.我有一个 jQuery 脚本,它自动将焦点向下移动到一列输入字段。

<script>
$(".input").keyup(function() {
  if (this.value.length > 0) {
    $('.input').eq($('.input').index(this) + 1).focus();
  }
});
</script>

and it works with this simple HTML它适用于这个简单的 HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="split4">
  <input type="tell" class="input" maxlength="1">
</div>
<div class="split4">
  <input type="tell" class="input" maxlength="1">
</div>
<h4>text</h4>
<div class="split4">
  <input type="tell" class="input" maxlength="1">
</div>
<div class="split4">
  <input type="tell" class="input" maxlength="1">
</div>

However, I am trying to apply it to a form rendered with WTForms like so and it doesn't work但是,我正在尝试将其应用于使用 WTForms 呈现的表单,但它不起作用

<form action="" method="post" novalidate>
                    <div style="text-align: center">
                        <h3>
                        {{ render_field(form.star, class="input", required=true, size=55, autofocus=true) }}
                        </h3> <br>
                        <h3>
                        {{ render_field(form.serial, class="input", required=true, size=55) }}
                        </h3> <br>
                        <h3>
                        {{ render_field(form.weight, class="input", required=true, size=55, onchange="submit()") }}
                        </h3> <br>
                    </div>
                </form>

I came across this answer but don't understand how to implement it.我遇到了这个答案,但不明白如何实现它。 If there is a simpler way of connecting HTML form inputs with Flask so that I can add to my local database, please let me know as well.如果有一种更简单的方法可以将 HTML 表单输入与 Flask 连接起来,以便我可以添加到本地数据库中,也请告诉我。

Thank you!谢谢!

Turns out, the script has to be inside the same block content as the form...Everything works now!事实证明,脚本必须与表单位于相同的块内容中......现在一切正常!

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

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