简体   繁体   English

从 Python Flask WTForms 字段触发 javascript

[英]Triggering javascript from Python Flask WTForms field

I have a Flask form (via WTForms) in my website, and am trying to enable or disable other fields in my form based on whether a user has selected or de-selected a checkbox (and do this without having to refresh the page) however, my attempts to achieve this by using the onchange or onclick event in the HTML hasn't worked.我的网站上有一个 Flask 表单(通过 WTForms),我试图根据用户是否选中或取消选中复选框来启用或禁用我的表单中的其他字段(并且无需刷新页面即可执行此操作)但是,我试图通过在 HTML 中使用 onchange 或 onclick 事件来实现这一点的尝试没有奏效。 My field is defined below (label and field)我的字段定义如下(标签和字段)

<div class="form-group row">
  {{ form.task_submit_sms.label(class_="col-sm-3 text-right control-label col-form-label") }}
  <div class="col-sm-1">
      {{ form.task_submit_sms(class_="custom-control custom-checkbox" onclick="enableSMSFields()") }}
  </div>

I have tried both onclick and onchange .我已经尝试过onclickonchange and get the following error:并得到以下错误:

jinja2.exceptions.TemplateSyntaxError: expected token ',', got 'onclick' jinja2.exceptions.TemplateSyntaxError:预期的标记“,”,得到“onclick”

Not sure if it's relevant, but here's my JavaScript:不确定它是否相关,但这是我的 JavaScript:

<script>
  // Lock SMS fields unless required
  function enableSMSFields()
  {
    if (document.getElementById('task_submit_sms').checked == true)
    {
      document.getElementById('task_sms_text').removeAttribute('disabled');
    }
    else
    {
      document.getElementById('task_sms_text').setAttribute('disabled','disabled')
    }
  }
</script>

This is a function inside the curly brackets, which you are passing arguments separated by commas (that is what the error is saying to you):这是大括号内的函数,您传递的参数以逗号分隔(这就是错误对您说的内容):

{{ form.task_submit_sms(class_="custom-control custom-checkbox" onclick="enableSMSFields()") }}

Have you tried inserting the comma separator?您是否尝试过插入逗号分隔符?

{{ form.task_submit_sms(class_="custom-control custom-checkbox", onclick="enableSMSFields()") }}

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

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