Django - Javascript not working in templates

旧巷老猫 提交于 2021-01-27 12:51:55

问题


I am trying to display an input field based on user choice selection. Learning from stackoverflow, I have written the following code:

<form action="/register/" method="post" enctype="multipart/form-data">{% csrf_token %}
<select name="designation" id="designation">
   <option value="faculty">Faculty</option>
   <option value="student">Student</option>
</select>

<div id="students" style="display:none;">
<input type="file" name="uploadFile">
</div>
</form>

<script type="text/javascript">
$('#designation').on('change',function(){
    if( $(this).val()==="student"){
    $("#students").show()
    }
    else{
    $("#students").hide()
    }
});
</script>

The code does work in https://jsfiddle.net/. However, when I am trying to use it in my django template its not working.(i.e., on selecting 'student' nothing appears). I have the following included as well in my base.html.

<script src="{% static 'jquery-3.2.1.min.js' %}"></script>
<script src="{% static 'js/bootstrap.min.js' %}" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

UPDATE: base.html: hideshow.js is the script.

<!DOCTYPE html>
{% load staticfiles %}
{% load static %}
<html lang="en"
      xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">       

    <link href="{% static 'css/bootstrap-dist.min.css' %}" rel="stylesheet">
    <link href="{% static 'css/ie10-viewport-bug-workaround.css' %}" rel="stylesheet">
    <script src="{% static 'JS/html5shiv.min.js' %}"></script>
    <script src="{% static 'JS/respond.min.js' %}"></script>
    <script src="{% static 'JS/hideshow.js' %}"></script>


    <link href="{% static 'css/carousel.css' %}" rel="stylesheet">
    <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    <link rel="stylesheet" href="{% static 'css/bootstrap-theme.min.css' %}" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
    <link rel="stylesheet" type="text/css" href="{% static 'css/sampleLogin.css' %}" />
</head>

<body>
{% include "messages_display.html" %}
<br />
{% include "navbar.html" %}

{% block content %}

{% endblock %}


register.html:

{% extends "base.html" %}
{% load crispy_forms_tags %}

{% block content %}    
<div class="container">
<div class="col-md-12">   
<div class="col-sm-7 col-sm-offset-2">
<h1>{{ title }}</h1><br />        
<form action="/register/" method="post" enctype="multipart/form-data">{% csrf_token %}
<select name="designation" id="designation">
   <option value="faculty">Faculty</option>
   <option value="student">Student</option>
</select>

<div id="students" style="display:none;">
<input type="file" name="uploadFile">
</div>
</form>

{% endblock content %}

Please help.


回答1:


First you have to include jquery.min.js file in html file. Then you have to keep js code in $(document).ready(function().then your code will work

 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<script type="text/javascript">
        $(document).ready(function() {
            $('#designation').on('change',function(){
                console.log($(this).val());
                if( $(this).val()==="student"){
                    $("#students").show()
                }
                else{
                    $("#students").hide()
                }
            });
        });
</script>

<select name="designation" id="designation">
   <option value="faculty">Faculty</option>
   <option value="student">Student</option>
</select>

<div id="students" style="display:none;">
<input type="file" name="uploadFile">
</div>


来源:https://stackoverflow.com/questions/45231022/django-javascript-not-working-in-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!