我正在用Django开发应用程序。
我有这个奇怪的问题。

在我的模板上,我有:

<div class="form-group">
  <input name="Data_inserimento_entry" type="date" class="form-control" id="date_to_turn_into_toda" >
</div>

<script type="text/javascript" src={% static "js/get_today_date.js" %}></script>
<script> get_today_date(date_to_turn_into_toda) </script>


在存储在静态> js> get_today_date.js的js文件get_today_date.js
我有

function get_today_date(id_data) {
var now = new Date();
var day = ("0" + now.getDate()).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);
var today = now.getFullYear()+"-"+(month)+"-"+(day);
document.getElementById(id_data).value = today;
}


当我运行服务器并加载模板时,在输入插槽中会显示今天的日期。我对此感到高兴。

如果我注释了模板中的两个javascript行中的任何一个,它将不再起作用。我对此感到高兴。

奇怪的部分来了

如果我更改ID,
像这样:

<div class="form-group">
  <input name="Data_inserimento_entry" type="date" class="form-control" id="date_to_turn_into_today" >
</div>

<script type="text/javascript" src={% static "js/get_today_date.js" %}></script>
<script> get_today_date(date_to_turn_into_today) </script>


它不起作用了。为什么?

即使我在两个脚本中都更改了函数名,例如:

<script type="text/javascript" src={% static "js/get_today_date.js" %}></script>
<script> get_today_date_ID(date_to_turn_into_today) </script>




function get_today_date_ID(id_data) {
var now = new Date();
var day = ("0" + now.getDate()).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);
var today = now.getFullYear()+"-"+(month)+"-"+(day);
document.getElementById(id_data).value = today;
}


它不起作用了。为什么?

调用javascript的语法正确吗?

我是否忘记在模板和javascript文件之外的其他位置更改ID或函数名称?

注意:javascript文件和javascript文件中的函数具有相同的名称(扩展名.js除外),这是问题吗?

更新:

在我的模型中,我有:

class mymodel(models.Model):

    Data_inserimento_entry = models.DateField(blank=False, null=False, default=timezone.now().date() )


更新:

在这里,我发布了整个模板aggiungi_terminologia.html,回复了要求更多解释的评论

    {% extends 'base.html'%}

    {% block content %}

        <h1>Aggiungi terminologia in massa</h1>

        <!-- scarica template glossario -->


        <p>
        <form method="get" action="static/files_for_download/template_glossario.xlsx">
        <div class="container"></div>
            <button type="submit" class="btn btn-success">Scarica template</button>
        </div>
        </form>
        </p>

        <div class="container">
        <small id="inputHelp" class="form-text text-muted">NOTA: Non rinominare le colonne del template.</small>
        </div>


        <br>


        <!-- carica glossario -->
        <p>
        <form class="container" method="POST" enctype="multipart/form-data" >
            {% csrf_token %}

            <div class="file-upload-wrapper" id="input-file-now">

                <small id="inputHelp" class="form-text text-muted">Seleziona il template compilato con la terminologia da caricare.</small>
                <input type="file"  name="uploaded_glossary" id="input-file-now" data-max-file-size="5M" class="file-upload">

                <br><br>


                <div class="form-group">

                     <input name="Data_inserimento_entry" type="hidden" class="form-control" id="date_to_turn_into_toda">
                </div>



                <button type="submit" class="btn btn-primary">Carica glossario</button>
            </div>

        </form>
        </p>


        <br><br><br><br>

    <!-- Django tag load static -->
    {% load static %}

    <!-- CSS -->
    <link rel="stylesheet" type="text/css" href={% static "css/searchbar_style.css" %}>
    <link rel="stylesheet" type="text/css" href={% static "css/upload_glossary_slot.css" %}>

    <!-- Javascript -->
    <script type="text/javascript" src={% static "js/get_today_date.js" %}></script>
    <script> get_today_date("date_to_turn_into_toda") </script>


    {% endblock %}


在这里,我发布了整个模板base.html:

{% load static %}

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- favicon -->
    <!-- <link rel="shortcut icon" href="{% static 'img/gestisco_logo_round_favicon2.png' %}" /> -->
    <link rel="shortcut icon" href="{% static 'img/metaglossary_favicon_3.png' %}" />

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

    <title>G</title>
  </head>
  <body>


      <nav class="navbar navbar-expand-lg navbar-dark bg-dark">

        <!-- Pagina iniziale -->
        <!-- questa nel tutorial non c è -->


        <a class="navbar-brand" href="{% url 'home' %}">GESTI.S.CO Interreg IT-CH</a>
          <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
          </button>

          <div class="collapse navbar-collapse" id="navbarSupportedContent">
            <ul class="navbar-nav mr-auto">

                <!-- glossario -->
              <li class="nav-item">
                <a class="nav-link" href="{% url 'glossario' %}">Glossario<span class="sr-only">(current)</span></a>

              </li>

              <!-- Aggiungi terminologia -->
              <li class="nav-item">
                <a class="nav-link" href="{% url 'aggiungi_terminologia' %}">Aggiungi terminologia</a>
              </li>

              <!-- Aggiungi glossario -->
              <li class="nav-item">
                <a class="nav-link" href="{% url 'aggiungi_glossario' %}">Aggiungi terminologia in massa</a>
              </li>





            </ul>


          </div>
        </nav>


        <br/>


<!-- SEZIONE MESSAGGI -->

        {% if messages %}

          {% for message in messages %}


            {% if insert_attempt_output == "errato" %}
            <!-- form compilato in modo NON valido - messaggio rosso -->

                <div class="alert alert-danger" role="alert">
                    <button class="close" data-dismiss="alert">
                        <small><sup>[X]</sup></small>
                    </button>
                    {{message}}
                  </div>

            {% else %}
            <!-- form compilato in modo valido - messaggio verde -->

            <div class="alert alert-success" role="alert">
                <button class="close" data-dismiss="alert">
                    <small><sup>[X]</sup></small>
                </button>
                {{message}}
              </div>


            {% endif %}

          {% endfor %}

       {% endif %}

      <br>

    <!-- formattazione del body -->

    <div class="container">



        {% block content %}



        {% endblock %}



      </div>


    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

      <!-- css -->
      <link rel="stylesheet" type="text/css" href={% static "css/top_navbar_style.css" %}>

  </body>
</html>

最佳答案

当我运行服务器并加载模板时,在输入插槽中会显示今天的日期。我对此感到高兴。


我不知道它如何工作,因为看起来您遇到的主要问题是将变量传递给函数get_today_date而不是字符串(id),即

<script> get_today_date(date_to_turn_into_today) </script>


呼叫

<script> get_today_date('date_to_turn_into_today') </script>

关于javascript - 更改HTML模板中的javascript函数名称或元素ID使其停止工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58451039/

10-10 00:18
查看更多