我正在使用脚本来控制我的Django表单集。我们的想法是动态生成,删除和重命名表单。如果页面是第一次加载,它工作正常,但如果我尝试提交页面,表单从POST返回,旧表单我无法删除。虽然我可以添加和删除新的。
function deleteForm(btn, prefix) {
var formCount = parseInt($('#id_' + prefix + '-TOTAL_FORMS').val());
if (formCount > 1) {
// Delete the item/form
$(btn).parents('.item-wrapper').remove();
var forms = $('.item-wrapper'); // Get all the forms
// Update the total number of forms (1 less than before)
$('#id_' + prefix + '-TOTAL_FORMS').val(forms.length);
var i = 0;
// Go through the forms and set their indices, names and IDs
for (formCount = forms.length; i < formCount; i++) {
$(forms.get(i)).find('*').each(function () {
updateElementIndex(this, prefix, i);
});
}
} // End if
else {
alert("You must have at least one item in your invoice.");
}
return false;
}
这是我的HTML:
{{ formset.management_form }}
{% for form in formset %}
<div class="item-wrapper">
<div class="item-inner-wrapper">
<div class="item-child-name">
{{ form.name.label_tag }}
<div class="ui-widget">{{ form.name }}</div>
</div>
</div>
</div>
<!-- OTHER INPUTS --->
<div class="item-action-button">
<a id="delete" href="#" class="button alt small special">Remove</a>
<a id="hide-desc" class="button alt small">+ Description</a>
</div>
{% endfor %}
答案 0 :(得分:0)
我发现调用该函数的按钮在脚本中有标识符作为其ID。因此,当脚本被调用时,它变得困惑,并且不知道哪个按钮正在进行呼叫。我只是更改了类的标识符,现在它运行良好。