【问题标题】:Django template get custom attribute from request OR "refresh" radio buttonsDjango模板从请求或“刷新”单选按钮获取自定义属性
【发布时间】:2011-11-26 06:53:15
【问题描述】:

我目前有一个包含 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 %}

问题是,因为它们的名称不同(这就是为什么,对吧?),如果我选择一个单选按钮,然后切换到另一个,第一个仍然显示为选中状态。

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

我能想到的唯一解决方法是插入一个脚本,为每个按钮分配一个检查事件,然后在检查后插入一个带有我想要的名称的隐藏输入,但这看起来很乱。

所以,我有没有办法: 从请求中获取按钮 ID,或者让按钮以某种方式“刷新”。

【问题讨论】:

    标签: django django-templates radio-button django-views forms


    【解决方案1】:

    保持名称不变,并将每个输入选项的值设置为答案 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 %}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-20
      • 1970-01-01
      • 1970-01-01
      • 2014-08-19
      • 2012-05-04
      • 1970-01-01
      • 2013-05-22
      • 2013-08-04
      相关资源
      最近更新 更多