Django模板从请求或“刷新”单选按钮获取自定义属性

时间:2022-06-02 20:33:19

I currently have a template containing an html form, with the lines:

我目前有一个包含html表单的模板,其中包含以下行:

{% for r in q1.responseoption_set.all %}
     <span class="r"><input type="{{ q1.answer_type }}" name="r{{ r.id }}" id="r{{ forloop.counter }}"/>
     <label {% if q1.answer_type == "text" %}class="textanswer"{% endif %}for="r{{ forloop.counter }}">{{ r.text }}</label></span><br>
{% endfor %}

problem is, because they don't all have the same name (that's why, right?), if I pick a radio button, and then switch to another one, the first one still shows as selected.

问题是,因为它们并不都具有相同的名称(这就是原因,对吧?),如果我选择一个单选按钮,然后切换到另一个,第一个仍显示为已选中。

However, at the moment, I need them to all have different names because I need to be able to identify the choices within my view, and as far as I can tell, all I can get from the request is [name, value], e.g. [r200, "on"]

但是,目前,我需要它们都有不同的名称,因为我需要能够识别我的视图中的选择,据我所知,我可以从请求获得的是[name,value],例如[r200,“on”]

The only way around this that I can think of is to insert a script that assigns a check event to each button, and then, once checked, inserts a hidden input with the name I want, but that seems messy.

我能想到的唯一方法是插入一个脚本,为每个按钮分配一个检查事件,然后,一旦检查,插入一个隐藏的输入,其名称我想要,但这似乎很麻烦。

SO, is there a way for me to either: get the button id from the request OR have the buttons "refresh" somehow as they are.

那么,对我来说有没有办法:从请求中获取按钮ID或按原样按下“刷新”按钮。

1 个解决方案

#1


1  

Keep the name the same, and set the value for each input choice to the answer id.

保持名称相同,并将每个输入选择的值设置为答案ID。

{% for r in q1.responseoption_set.all %}
     <span class="r"><input type="{{ q1.answer_type }}" name="{% questionId %}" value="r{{ r.id }}" id="r{{ forloop.counter }}"/>
     <label {% if q1.answer_type == "text" %}class="textanswer"{% endif %}for="r{{ forloop.counter }}">{{ r.text }}</label></span><br>
{% endfor %}

#1


1  

Keep the name the same, and set the value for each input choice to the answer id.

保持名称相同,并将每个输入选择的值设置为答案ID。

{% for r in q1.responseoption_set.all %}
     <span class="r"><input type="{{ q1.answer_type }}" name="{% questionId %}" value="r{{ r.id }}" id="r{{ forloop.counter }}"/>
     <label {% if q1.answer_type == "text" %}class="textanswer"{% endif %}for="r{{ forloop.counter }}">{{ r.text }}</label></span><br>
{% endfor %}