admin管理员组文章数量:1330376
Is there a way to pass "onclick" onto a WTForm field? I'd like to enable/disable a field depending on whether a WTF checkbox is selected. But the HTML from WTForms does not create or have an "onclick" parameter.
I have a form:
class test(Form):
checkbox=BooleanField('Checkbox')
required=TextField('Required if checked')
and I have the JS:
function disablefld(){
cb=document.getElementById('checkbox').checked;
document.getElementById('required').disabled=!cb;
}
The HTML WTForms generated for the checkbox is:
<input id="checkbox" name="checkbox" type="checkbox" value="y">
. It doesn't work becauseonclick
is not present.
I've tried checkbox=BooleanField('Checkbox', onclick="disablefld()")
but it is an unexpected argument. Is this possible or should I just make the form in pure html?
Is there a way to pass "onclick" onto a WTForm field? I'd like to enable/disable a field depending on whether a WTF checkbox is selected. But the HTML from WTForms does not create or have an "onclick" parameter.
I have a form:
class test(Form):
checkbox=BooleanField('Checkbox')
required=TextField('Required if checked')
and I have the JS:
function disablefld(){
cb=document.getElementById('checkbox').checked;
document.getElementById('required').disabled=!cb;
}
The HTML WTForms generated for the checkbox is:
<input id="checkbox" name="checkbox" type="checkbox" value="y">
. It doesn't work becauseonclick
is not present.
I've tried checkbox=BooleanField('Checkbox', onclick="disablefld()")
but it is an unexpected argument. Is this possible or should I just make the form in pure html?
1 Answer
Reset to default 7You need to pass the extra arguments while rendering your form.
{% block content %}
{{ form.checkbox(onchange="doStuff()") }}
{{ form.required() }}
<script>
function doStuff(){
var checked = document.getElementById('checkbox').checked
if (checked){
document.getElementById('required').disabled = true
} else {
document.getElementById('required').disabled = false
}
}
doStuff()
</script>
{% endblock %}
本文标签: htmlWTFormsJavascript pass onclick to WTF fieldStack Overflow
版权声明:本文标题:html - WTForms-Javascript: pass onclick to WTF field - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742266978a2443587.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论