【发布时间】:2016-04-22 10:13:04
【问题描述】:
我在 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',)
downcall.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 模板中的 {% load staticfiles %} ...相同的步骤适用于另一个模板表单,但没有这个..
这里是 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 库,尽管它不起作用。
【问题讨论】:
-
“它不工作”是什么意思?
-
我正在尝试为 html 中的 tat 元素设置最小值...但是 jquery 文件没有运行。
-
你需要清楚地说出你想做什么,你尝试过什么,你得到什么结果,你期待什么结果。
-
我尝试使用这种方法:class CustomerForm(forms.ModelForm): 。 . class Media: js = ('js/customer_detail.js',) 然后将它像 {{
.media}} 包含在您的模板中,然后在 customer_detail.js 中编写您的 JavaScript 代码,我只添加了 alert() jquery中的函数来测试它是否正在运行,但它没有在我的模板中运行.. -
我从 base.html 继承 js 文件,对于 customer_detail.html,jquery 正在运行,而对于其他模板,jquery 没有运行并且没有显示任何错误。
标签: javascript jquery python html django