【问题标题】:I wanna set type's number to id of select tag我想将类型的编号设置为选择标签的 id
【发布时间】:2017-09-27 04:42:51
【问题描述】:

我想将类型的编号设置为选择标签的 id。 我在浏览器中理想的 html 是

 <select id="mainDD" data-placeholder="Choose" class="chzn-select" style="width:600px;">
      <option value="0">---</option>
      <option value="1">a</option>
      <option value="2">b</option>
    </select>
    <select name="type" id="type1">
      <option value="1">a1</option>
      <option value="2">a2</option>
    </select>
    <select name="type" id="type2">
      <option value="5">b1</option>
      <option value="6">b2</option>
    </select>

现在浏览中的实际 html 是

   <select id="mainDD" data-placeholder="Choose" class="chzn-select" style="width:600px;">
            <option>---</option>  
            <option>a</option> 
            <option>b</option>  
    </select>

    <select name="type" id="type">
        <option value="0">---</option>
        <option value="1">a1</option>   
        <option value="2">a2</option>   
    </select>

    <select name="type" id="type">
        <option value="5">---</option>
        <option value="6">b1</option>
        <option value="7">b2</option>  
    </select>

id="type" of 没有每个数字,但我不知道如何使用 Django 的模板添加这些数字。 我在 index.html 中写了

<form method="post" action="">
    <select id="mainDD" data-placeholder="Choose" class="chzn-select" style="width:600px;">
    {% for i in json_data.items.values %}
            <option>{{ i }}</option>
    {% endfor %}
    </select>


    {% for key, values in preprocessed %}
    <select name="type" id=type>
    {% for counter, value in values %}
        <option value="{{ counter }}">{{ value }}</option>
    {% endfor %}
    </select>
    {% endfor %}
    </form>

我应该写什么?我该如何解决这个问题?

【问题讨论】:

    标签: python html django


    【解决方案1】:

    使用forloop.counter 代替计数器:

    {% for key, values in preprocessed %}
    <select name="type" id=type{{forloop.counter}}>
      {% for counter, value in values %}
      <option value="{{ forloop.counter }}">{{ value }}</option>
      {% endfor %}
    </select>
    {% endfor %}
    

    【讨论】:

      【解决方案2】:
      <form method="post" action="">
          <select id="mainDD" data-placeholder="Choose" class="chzn-select" style="width:600px;">
          {% for i in json_data.items.values %}
                  <option>{{ i }}</option>
          {% endfor %}
          </select>
      
      
          {% for key, values in preprocessed %}
              <select name="type" id=type-{{ key }}>
              {% forvalue in values %}
                  <option value="{{ forloop.counter0 }}">{{ value }}</option>
              {% endfor %}
              </select>
          {% endfor %}
      </form>
      

      最好在类型中使用您的密钥,以便您以后可以轻松引用它。也不需要单独的计数器变量。 Django 模板标签为此提供了 forloop.counter。 forloop.counter0 以 0 开始计数器索引,forloop.counter 以 1 开始计数器索引。希望这能消除您的疑问。

      参考 - https://docs.djangoproject.com/en/1.11/ref/templates/builtins/#for

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-11
        • 2021-06-19
        • 2013-05-07
        • 2012-08-14
        • 1970-01-01
        相关资源
        最近更新 更多