简体   繁体   English

Django-$(document).ready(function(){}不会触发,但会加载JQuery

[英]Django - $(document).ready(function() {} does not trigger, but JQuery is loaded

my template: 我的模板:

{% extends "base.html" %}
{% block content %}
{% load static %}

<script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.js">
$(document).ready(function() {
    alert("I am an alert box!");
});
$(function() {
    $( ".datepicker" ).datepicker({
        changeMonth: true,
        changeYear: true,
    });
});
</script>
<form action="{% url 'transaction_create' %}" method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" name="save" value="Save transaction">
</form>

{% endblock %}

The google chrome inspector shows that JQuery loads, but no alert box appears nor does the datepicker work (the class on the field is set correctly). 谷歌浏览器检查器显示已加载JQuery,但没有出现警告框,日期选择器也不起作用(字段上的类设置正确)。 Why does the Javascript not work? 为什么Javascript不起作用?

You need another opening <script> tag because the one you have in your code is solely dedicated to loading jQuery. 您需要另一个打开的<script>标记,因为您的代码中的标记专门用于加载jQuery。 MDN states that: MDN指出:

If a script element has a src attribute specified, it should not have a script embedded inside its tags. 如果脚本元素指定了src属性,则其标签中不应嵌入脚本。

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

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