Django ChoiceField RadioSelect 表单中的小部件,测试模板中选择的元素
我在ChoiceField中有2个无线电按钮,我想显示模板的某些部分,根据女巫无线电按钮。
以下:
- form.py
class ctdform(forms.form): stolopt_name = forms.charfield(max_length = 100) rb = forms.booleanfield(必需= false,label ='rb') mr = forms.booleanfield(必需= false,label ='mr') 选择= [('rb','rb'),('Mr','Mr')] Analyse_Type = forms.choicefield(选择=选择,窗口= forms.radioselect)
- 模板html
... {{form.analyse_type}} 在这里,我想测试选择哪个按钮,并根据选择的方式显示模板 类似:{%如果form.analyse_type。 == true%} ...
我用form.analyse_type来测试很多语法。像form.analyse_type.field.widget.Choices一样,可以在循环中具有每个选择。 ,但是我没有找到正确的人返回选定的radiobutton ...
也许这种方式不是做我想做的正确的人。
如果您有任何想法,解决方案谢谢;)
用户2497126 编辑 感谢所有提示:)!
我有一个错误未发现的错误:get_lements: - >表格[data-sb-form-api-toke]似乎与对象分析符合链接
,按照html元素进行打印屏幕。我疲倦了不同的综合
让radiovalue = $(“输入[name ='Analyse_type']:checked”)。value();
我还将我的HTML代码放在
{% extends "index.html" %}
{% load static %}
{% block content %}
<div style="padding-left: 30%;" class="col-lg-6 col-xl-6"
<h2 class="h4 fw-bolder">
Analyse Type
</h2>
<br>
{{ form.analyse_type }}
<br> </div>
<div id="template-one" style="display:none;">
<div style="padding-right: 30%;" class="col-lg-6 col-xl-6">
<h2 class="h4 fw-bolder">
TEST
</h2>
</div>
</div>
{% endblock %}
{% block javascript %}
<script type="text/javascript">
$(document).ready(function () {
$(".custom-control-input").change(function () {
let radioValue = $("input[name='analyse_type']:checked").value();
let templateOne = document.getElementById('template-one')
if (radioValue == "rb") {
$("#template-one").show();
} else {
$("#template-one").hide();
}
});
});
</script>
{% endblock javascript %}
edit
的情况下,如果我做了
console.log($(“输入[name ='Analyse_type']:checked”)。val())
在Chrome的调试器中我有RB的返回。 但是错误仍然没有结果。 我还更改了html,将模板一个包含在表单的同一div中,就像你的示例一样 问题似乎在这里
$(“。自定义控制输入”)。更改(function(){
i替换.custom或select或根据一些论坛答案选择#id_analyse_type,但是我没有结果
感谢您的时间和您的帮助:)
< em> 编辑
在这里解决方案:
{% block javascript %}
<script type="text/javascript">
$(document).ready(function () {
let radioValue = $("input[name='analyse_type']:checked").val();
if (radioValue == "rb") {
$("#template-one").show();
} else {
$("#template-one").hide();
}
$('#id_analyse_type').change(function () {
let radioValue = $("input[name='analyse_type']:checked").val();
if (radioValue == "rb") {
$("#template-one").show();
} else {
$("#template-one").hide();
}
});
});
</script>
{% endblock javascript %}
谢谢您的帮助!
I have 2 radio buttons in a ChoiceField and I would like to display some parts of the template, depending of witch radio button is selected.
Following :
- form.py
class CtdForm(forms.Form): protocol_name = forms.CharField(max_length=100) rb = forms.BooleanField(required=False, label='RB') mr = forms.BooleanField(required=False, label='MR') CHOICES = [('rb' ,'RB'), ('mr', 'MR')] analyse_type = forms.ChoiceField(choices=CHOICES, widget=forms.RadioSelect)
- template html
... {{ form.analyse_type }} Here I would like to test which button is selected and display the template depending of the selection something like : {% if form.analyse_type.? == true %} ...
I test a lot of syntaxe with form.analyse_type.? like form.analyse_type.field.widget.choices to have each choices in a loop ect. , but I do not found the right one returning the selected radiobutton...
Maybe this way is not the right one to do what I want.
If you have any idea, solution thank you ;)
Edit for user2497126
Thanks for all tips :) !
I have an error Uncaught Error: GET_ELEMENTS: -> form[data-sb-form-api-token] seems to be in link with the object analyse_type
As requested, following a print screen with the html element.I tired different synthaxe like
let radioValue =$("input[name='analyse_type']:checked").value();
I also put my HTML code in case
{% extends "index.html" %}
{% load static %}
{% block content %}
<div style="padding-left: 30%;" class="col-lg-6 col-xl-6"
<h2 class="h4 fw-bolder">
Analyse Type
</h2>
<br>
{{ form.analyse_type }}
<br> </div>
<div id="template-one" style="display:none;">
<div style="padding-right: 30%;" class="col-lg-6 col-xl-6">
<h2 class="h4 fw-bolder">
TEST
</h2>
</div>
</div>
{% endblock %}
{% block javascript %}
<script type="text/javascript">
$(document).ready(function () {
$(".custom-control-input").change(function () {
let radioValue = $("input[name='analyse_type']:checked").value();
let templateOne = document.getElementById('template-one')
if (radioValue == "rb") {
$("#template-one").show();
} else {
$("#template-one").hide();
}
});
});
</script>
{% endblock javascript %}
EDIT
if I do a
console.log($("input[name='analyse_type']:checked").val())
in the debugger of chrome I have a return of rb.
But the error is still there with no result.
I also change the html, include the template-one in the same div of the form like your example
The problem seems here
$(".custom-control-input").change(function () {
I replace .custom by select or select#id_analyse_type, based on some forum answers, but I have no result
Thanks for your time and your help :)
EDIT
Here the solution :
{% block javascript %}
<script type="text/javascript">
$(document).ready(function () {
let radioValue = $("input[name='analyse_type']:checked").val();
if (radioValue == "rb") {
$("#template-one").show();
} else {
$("#template-one").hide();
}
$('#id_analyse_type').change(function () {
let radioValue = $("input[name='analyse_type']:checked").val();
if (radioValue == "rb") {
$("#template-one").show();
} else {
$("#template-one").hide();
}
});
});
</script>
{% endblock javascript %}
Thank you for your help !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我试图适应
更改功能的问题的编辑答案(即选择无线电时)检查并用无线电输入中的值替换类值。另外,请确保您在实例化变量radiovalue时提供正确的名称,我使用“ Analyze_Type”来示例,但请检查并确认无线电输入的正确名称值。
您可以在模板中使用这样的JS实现它
Here is an edited answer which I have tried to adapt to your question
For the change function (i.e when selecting the radio) inspect and replace the class value with the one in your radio input. Also ensure you supply the correct name when instantiating the variable radioValue, I have used 'analyze_type' for the sake of the example but inspect and confirm the correct name value of the radio input.
You can achieve it using JS like this in your template