我正在使用jquery.formset.js尝试并动态地向我的窗体集中添加和删除行。
https://github.com/elo80ka/django-dynamic-formset
我在Chrome开发工具中遇到了一个错误:
Uncaught TypeError: $(...).formset is not a function(…)我不明白为什么..。下面是在底部定义脚本并链接到jquery.formset.js的模板。
{% extends 'project/base.html' %}
{% block title %}...{% endblock %}
{% load staticfiles %}
{% block content %}
<form id="time-form" method="POST" action="" class="dynamic-form" enctype="multipart/form-data">{% csrf_token %}
<div style="overflow-x:auto; padding-bottom:0;">
<table id="timesheet" class="tables">
<tbody>
<tr class="table-header">
<th>Date</th>
<th colspan="2">Description</th>
</tr>
{{ time_formset.management_form }}
{% for form in time_formset %}
<tr class="formset_row">
{% for field in form.visible_fields %}
<td>
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% if forloop.last %}
{% if form.instance.pk %}<td>{{ form.DELETE }}</td>{% endif %}
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
</div>
</br>
<input type = "submit" id = "save" value = "Save">
</form>
<script type="text/javascript" src='/media/jquery.formset.js'></script>
<script type="text/javascript" src="{% static 'app/jquery.js' %}"></script>
<script src="{% static 'app2/jquery-ui/external/jquery/jquery.js' %}"></script>
<script src="{% static 'app2/jquery-ui/jquery-ui.js' %}"></script>
...
<script>
$(function() {
//ERROR RAISED HERE//
$('#time-form div table#timesheet tbody tr.formset_row').formset({
prefix: '{{ time_formset.prefix }}'
});
});
</script>我已经检查了js文件的链接情况,并在文档就绪时发出了警报,这是很好的。我已经检查了元素$存在并返回true。我不知道这里出了什么问题?
注意:我的表单集是InLineFormSet
TimeFormSet = inlineformset_factory(TimeSheet, Time, form=TimeForm, extra=1, can_delete=True)救命?
发布于 2017-04-19 16:34:58
尝试在formset插件之前加载jQuery。
<script type="text/javascript" src="{% static 'app/jquery.js' %}"></script>
<script type="text/javascript" src='/media/jquery.formset.js'></script>使用/media/进行插件看起来很不寻常,我希望看到静态标记与app/jquery.js相同。
https://stackoverflow.com/questions/43501162
复制相似问题