Django Admin javascript加载但不起作用

时间:2019-06-15 12:35:23

标签: javascript jquery django django-templates

我从https://jsfiddle.net/emkey08/zgvtjc51那里获取了此文件,请尝试用 Django管理员。 代码是

if (!$) {
    $ = django.jQuery;
}
$(function($) {
  $.fn.inputFilter = function(inputFilter) {
    return this.on("input keydown keyup mousedown mouseup select contextmenu drop", function() {
      if (inputFilter(this.value)) {
        this.oldValue = this.value;
        this.oldSelectionStart = this.selectionStart;
        this.oldSelectionEnd = this.selectionEnd;
      } else if (this.hasOwnProperty("oldValue")) {
        this.value = this.oldValue;
        this.setSelectionRange(this.oldSelectionStart, this.oldSelectionEnd);
      }
    });
  };
}(django.jQuery));


$("#id_fin").inputFilter(function(value) {
  return /^\d*$/.test(value); });

现在,此代码已加载但不起作用,但可以从控制台运行。还有其他人面临相同的问题,并且建议的答案是将脚本移到DOM的末尾。 change_form.html扩展如下。

{% extends "admin/change_form.html" %}
{% load static %}

{% block admin_change_form_document_ready %}{{ block.super }}
    <script type="text/javascript" src="{% static 'prop/js/number_validate.js' %}"></script>
{% endblock %}

仍然最终结果是相同的。有人可以提出一些建议。

$.fn.inputFilter = function (inputFilter) {
    return this.on("input keydown keyup mousedown mouseup select contextmenu drop", function () {
        if (inputFilter(this.value)) {
            this.oldValue = this.value;
            this.oldSelectionStart = this.selectionStart;
            this.oldSelectionEnd = this.selectionEnd;
        } else if (this.hasOwnProperty("oldValue")) {
            this.value = this.oldValue;
            this.setSelectionRange(this.oldSelectionStart, this.oldSelectionEnd);
        }
    });
};

$(function () {

    $("#id_fin").inputFilter(function (value) {
        return /^\d*$/.test(value);
    });

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Integer (both positive and negative  123):<input id="id_fin">

0 个答案:

没有答案