jquery在Django中不起作用

jquery is not working in Django

本文关键字:不起作用 Django jquery      更新时间:2023-09-26

我正在使用jQuery,但无法使其在模板上工作。

forms.py

class DowncallForm(forms.ModelForm):
engineer = Engineer.objects.all()
class Meta :
    model = Call
    fields = ['problem_type', 'status', 'remarks', 'call_date', 'exp_closing_time', 'tat',
              'assigned_engineer']
class Media:
    js = ('js/downcall_validation.js',)

下调.html

<html>
<head>
    <title>Downcall Detail</title>
      <script type="text/javascript" src="{% static "js/downcall_validation.js" %}"></script>
      <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
      <script src="//code.jquery.com/jquery-1.9.1.js"></script>
      <script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
</head>
<body>
    {% block content %}
        {{ DowncallForm.media }}
 <form method="post" action="" id="downcall_register_form" enctype="multipart/form-data">
    {% csrf_token %}
 <div id="div_id_tat" class="form-group">
     <label for="id_tat" class="control-label  requiredField">
     Tat<span class="asteriskField">*</span>
     </label>
     <div class="controls ">
     <input class="numberinput form-control" id="id_tat" name="tat" type="number" />
     </div>
     </div>

我已经将媒体文件包含在我的 forms.py 中,并将静态文件加载到 HTML 模板中为 {% 加载静态文件 %} ...相同的步骤适用于另一个模板表单,但不适用于此模板表单。

这是jquery文件.. downcall_validation.js

$(document).ready.(function() {
    alert("hai");
  //  Setup form validation on the #register-form element
    //  downcall_register_form==> form id
  $("#downcall_register_form").validate({
      //$("#id_tat").rules("add", {min: 1}),
      // Specify the validation rules
      //id_tat==> id of the input element
      rules: {
          id_tat: {
              min: 1
          }
      },
      // Specify the validation error messages
      messages: {
          id_tat: "Please enter a positive number"
      }
      //    submitHandler: function(form) {
      //        form.submit();
      //    }
      //});
  });

});

我也包含了javascript库,即使它不起作用。

你应该更具体地说明"它不起作用"是什么意思。

然而,一个明显的错误是,在加载jQuery本身之前,你包含了自己的脚本文件 - 这取决于jQuery,这将失败。将脚本标记移动到 jquery 标记之后。

此外,您还包含来自不同目标的两次 jQuery 和 jquery-valid。别这样;选择一个并使用它。

在你对 jQuery ready函数的调用中有一个点......

$(document).ready.(function() {

应该是...

$(document).ready(function () {