简体   繁体   English

启用在Django中选择下拉值时禁用文本字段

[英]enable disable a textfield on the selection of dropdown value in Django

I have a register page in Django which contains some textfield and a dropdown value. 我在Django中有一个注册页面,其中包含一些文本字段和一个下拉值。

models.py models.py

class UserInformation(models.Model):
      firstName = models.CharField(max_length=128)
      lastName = models.CharField(max_length=128)
      emailAddress = models.EmailField(max_length=128)
      phoneNumber = models.CharField(max_length=128)    
      orchidNumber = models.CharField(max_length=128)
      institution = models.CharField(choices = [("Inst1","Inst1"), ("Inst2","Inst2"),("Other","Other")], max_length=128)
      otherInstitute = models.CharField(default="N/A",max_length=128)
      cstaPI = models.CharField(max_length=128)

Currently all the fields are displaying on my register page. 当前所有字段都显示在我的注册页面上。 But the field 但是领域

otherInstitute

should be displayed only when a user selects 仅在用户选择时显示

**Other** in institution dropdown.

register.html register.html

{% extends "base.html" %}
{% block title %}User Registration{% endblock %}
{% block head %}User Registration{% endblock %}
{% block content %}

<form method="post" action=".">{% csrf_token %}
    <table border="0">
        {{ form.as_table }}
    </table>
    <input type="submit" value="Register" />
</form>
{% endblock %}

views.py views.py

@csrf_protect
def register(request):
    if request.method == 'POST':
        form = UserInformationForm(request.POST)
        form.save()
        return HttpResponseRedirect('/register/success/')
    else:
        form = UserInformationForm()
        variables =  { 'form': form }

    return render(request, 'registration/register.html',variables)

I am not sure how to implement the logic and where I should implement the logic in Django. 我不确定如何实现逻辑以及在Django中应在哪里实现逻辑。

EDIT 编辑

register.html register.html

 {% extends "base.html" %}
 {% block title %}User Registration{% endblock %}
 {% block head %}User Registration{% endblock %}
 {% block content %}

 <script type="text/javascript">
$(document).ready(function () {
    $('[name="institution"]').change(function () {
        var end = this.value;

        if (end == 'Other') {
            $('[name="otherInstitute"]').show();
        }
        else {
            $('[name="otherInstitute"]').hide();
        }
    })
});
</script>

 <div class="row">
    <section id="registerForm">
          <form method="post" action=".">{% csrf_token %}
          <div class="form-group">
                  <label for="id_firstName" >First Name (*)</label>
                       {{ form.firstName }}
          </div>
          <div class="form-group">
                  <label for="id_lastName" >Last Name (*)</label>                      
                       {{ form.lastName }}                       
          </div>
          <div class="form-group">
                  <label for="id_email">Email Address (*)</label>                      
                       {{ form.emailAddress }}
          </div>
          <div class="form-group">
                  <label for="id_phone" >Contact Number</label>
                       {{ form.phoneNumber }}
          </div>
          <div class="form-group">
                  <label for="id_orchid">Orchid ID  (<a href="https://orcid.org/register">Get Orchid ID</a>)</label>
                       {{ form.orchidNumber }}   
          </div>
          <div class="form-group">
                  <label for="id_intitution">Institution (*)</label>                                    
                       {{ form.institution }}
          </div>
          <div id="otrInst" class="form-group">
                  <label for="id_otherintitution" >Other Institution</label>
                       {{ form.otherInstitute }}
          </div>
          <div class="form-group">        
               <label for="id_ctsaPI">CTSA Prinicipal Investigator (*)</label>                                    
                 {{ form.cstaPI }}

          </div> <br/><br/><br/><br/>
          <div class="form-group">
                    <input type="submit" value="Register" />
          </div>

   </form>
 </section>

Added Jquery in my base.html 在我的base.html中添加了Jquery

<script type="text/javascript" src="{{ STATIC_URL }} /static/jquery-1.10.2.js"></script> 
 <script type="text/javascript" src="{{ STATIC_URL }} /static/jquery-1.10.2.min.js"></script> 

You should use JQuery or JavaScript. 您应该使用JQuery或JavaScript。 define a method in javascript and associate it with onChange event of dropdown field. 在javascript中定义方法,并将其与下拉字段的onChange事件关联。

 $('[name="institution"]').change(function () { var end = this.value; if (end == 'Other'){ $('[name="otherInstitute"]').show(); } else { $('[name="otherInstitute"]').hide(); } } 

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

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