按下按钮后,TOTAL_FORMS 剂量更新,我有相同的数字 1,我所有的行都像下面的报价一样。
$(document).ready(function() {
$('.add-item').click(function(ev) {
ev.preventDefault();
var count = $('#items-form-container').children().length;
var tmplMarkup = $('#item-template').html();
var compiledTmpl = tmplMarkup.replace(/__prefix__/g, count);
$('div#items-form-container').append(compiledTmpl);
// update form count
$('#id_item_items-TOTAL_FORMS').attr('value', count + 1);
// some animate to scroll to view our new form
$('html, body').animate({
scrollTop: $("#add-item-button").position().top-200
}, 800);
});
});
我添加了 3 个表格,但总数更新了。
&form-TOTAL_FORMS=1
&form-INITIAL_FORMS=0
&form-MIN_NUM_FORMS=0
&form-MAX_NUM_FORMS=1000
&form-0-good=5&form-0-count=14
&form-TOTAL_FORMS=1
&form-INITIAL_FORMS=0
&form-MIN_NUM_FORMS=0
&form-MAX_NUM_FORMS=1000
&form-0-good=6&form-0-count=155
&form-TOTAL_FORMS=1
&form-INITIAL_FORMS=0
&form-MIN_NUM_FORMS=0
&form-MAX_NUM_FORMS=1000
&form-0-good=12&form-0-count=55
我需要的。
'form-TOTAL_FORMS': '2',
'form-INITIAL_FORMS': '0',
'form-MAX_NUM_FORMS': '',
'form-0-title': 'Test',
'form-0-pub_date': '1904-06-16',
'form-1-title': 'Test',
'form-1-pub_date': '1912-06-23',
UPD: 你怎么说我添加 HTML 代码。主要问题我使用 djnago 生成所有数据我尝试创建 js fiddle
<script type="text/html" id="item-template">
<div id="item-__prefix__">
{{ formset.as_p }}
</div>
</script>
<!--Example -->
<form action="" method="post">
{% csrf_token %}
{{csrf_token}}
{{ formset.management_form }}
<div id="items-form-container">
{% for item_form in formset %}
<div id="item-{{ forloop.counter0 }}">
{{ item_form.id }}
{{ item_form.as_p}}
</div>
{% endfor %}
</div>
<a href="#" id="add-item-button" class="btn btn-info add-item">Add Item</a>
<button type="submit">OK</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('.add-item').click(function(ev) {
ev.preventDefault();
var count = $('#items-form-container').children().length;
var tmplMarkup = $('#item-template').html();
var compiledTmpl = tmplMarkup.replace(/__prefix__/g, count);
$('div#items-form-container').append(compiledTmpl);
// update form count
$('#id_item_items-TOTAL_FORMS').attr('value', count + 1);
// some animate to scroll to view our new form
$('html, body').animate({
scrollTop: $("#add-item-button").position().top-200
}, 800);
});
});
</script>