6

我尝试在 stackoverflow 上按照选择列表中选择的选项的一些示例进行操作,但仍然无法正常工作。

这是我的代码片段

<select name="topic_id" style="width:90%">
    {% for t in topics %}
        <option value="{{t.id}}" {% if t.id == topic_id %} selected="selected" {% endif %}>{{t.title}}{{t.id}}</option>
    {% endfor %}
</select>

我从 views.py 中的 param['topic_id'] 中获取了 topic_id。

谢谢!

4

2 回答 2

7

而不是selected="selected"仅仅使用selected.

<option value="{{t.id}}"{% if t.id == topic_id %} selected{% endif %}>{{t.title}}{{t.id}}</option>

如果它不起作用,可能有两个原因:

  • 在主题中没有这样的主题id等于topic_id
  • t.id并且topic_id可能是不同的类型。转换topic_idint
于 2012-12-24T13:10:50.313 回答
1

这对我有用

<select class="form-control" id="exampleSelect2" name="nivel">
                {% for key, value in types %}
                    <option value="{{ key }}" {% if institution.nivel == key %} selected {% endif %}>{{ value }}</option>
                {% endfor %}
</select>
于 2018-04-05T16:19:07.313 回答