简体   繁体   English

如何设置复选框检查值是否为是(Django)

[英]How to Set Checkbox Checked if Value Is Yes (Django)

I have a form which I am prefilling through a GET request. 我有一个表格,我通过GET请求预先填写。 I am setting the value of a checkbox to either Yes or No . 我将复选框的值设置为YesNo I would like to set the checkbox to checked if the value is Yes . 我想设置复选框以checked值是否为Yes

script 脚本

<script>
    if ($('#cisco').val() == 'Yes') {
        $('#cisco').prop('checked', true);
    }
</script>

form.html form.html

<div class="field">
    <div class="control">
        <label class="checkbox">
            <input type="checkbox" name="cisco" id="cisco"
                   value="{{ request.GET.cisco }}">Cisco:
        </label>
    </div>
</div>

Do an if else statement in the HTML. 在HTML中执行if else语句。 You don't need the javascript. 你不需要javascript。

<div class="field">
  <div class="control">
    <label class="checkbox">

      {% if request.GET.cisco == "Yes" %}
        <input type="checkbox" name="cisco" id="cisco" checked> Cisco:
      {% else %}
        <input type="checkbox" name="cisco" id="cisco"> Cisco:
      {% endif %}

    </label>
  </div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM